Useful Beginner Tips-VBForums
Results 1 to 5 of 5

Thread: Useful Beginner Tips

  1. #1

    Thread Starter
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Useful Beginner Tips

    I see a lot of beginners posting in these forums, or maybe even folks like me who are former programmers who are getting back into the swing of things after a long break and need some helpful reminders of the little, simple things. So I decided to create a short topic of helpful tips that you can use to make your life easier when creating an application, be it a small private app or a large, widely-used app. So here are some simple, basic timesavers:


    Using modules to make things easy!
    If you're ever going to use a simple proceedure or string/variable multiple times in your app, it's a good idea to declare it in a module. Things like a custom app name that is different from the app name you set in your app's properties, build number, app status(beta, alpha, etc.), app author, etc. I use some things that are easy for me to remember and easy to change if I need to. For example, in a module's declarations:

    VB Code:
    1. Public Const prjName = "My Custom Application Title"
    2. Public Const prjBuild = 1.001
    3. Public Const prjStatus = "Beta"
    4. Public Const prjAuthor = "Brendan M. Davis"
    5. Public Const prjCompany = "FireFly Media, LLC."

    This makes it easy for you to change things quickly without having to go through your entire app looking for it. For example, if you used:

    VB Code:
    1. MsgBox "A message box message goes here", vbOkOnly, prjName

    ...you could have every message box display your application name the same way. Thus, if you wanted to change it sometime down the road, it would be as simple as changing the Public Const prjName = "Something Different"

    This is also EXTREMELY useful for things that VB does not have a place for you to enter. For example, your form background colors. Want to change them without having to go through each one individually? Or, maybe you want to let the user change it themself and save it that way. Creating a function for it, and then using that in the Form_Load area will make it easy for you (and the user, if other people will be using your app) to change it with 1 function call. For example:

    VB Code:
    1. Public Function frmBGColor()
    2. frmBGColor = 'Whatever color you choose, or create a LoadSetting to make it customized
    3. End Function


    Some general rules of thumb..

    When you're using a custom set string, integer, label, etc., it's always good to declare it as such before proceeding with any code that uses it. For example, if you want to create a string and set a specific property to it, the WRONG way to use it would be:


    VB Code:
    1. Public Sub Form_Load
    2.  
    3. myCustomString = "Hello, UserName!"
    4.  
    5. Me.Caption = Mid$(myCustomString, 8, Len(myCustomString)) 'To get the username
    6.  
    7. End Sub

    You didn't declare the custom string as a string, which is bad practice. The best way to use it would be something like:


    VB Code:
    1. Public Sub Form_Load
    2.  
    3. Dim myCustomString As String 'This set it to a string
    4.  
    5. myCustomString = "Hello, UserName!"
    6.  
    7. Me.Caption = Mid$(myCustomString, 8, Len(myCustomString)) 'To get the username
    8.  
    9. End Sub

    It's a small addition, but it will ensure your app runs the best that it can.


    Don't get module crazy!
    Modules are great timesavers, and they're key to making a great app if you know how to use them correctly. But using them TOO much is a bad thing. Creating a function that you're going to use in several different areas, that happens to display/not display MsgBox's is not good. Trust me, it is for some things... not for most. If you create a function that handles all the error messages and dialoge boxes for you, you may come accross some trouble later on when you want to use that function in a different way than what you coded it for. The result? You'll have to either 1) go back and change the fucntion, and thus change the original useage of the function(i.e. headache city), or 2) you'll have to write another similar function with/without the msg handling that you want/don't want. The way around that is to create a Boolean type for the function, something like:

    VB Code:
    1. Private Function CheckMyList(lItem as String, lList as ListBox, Optional showMsgs as Boolean = False)
    2.  
    3. 'Function code here
    4.  
    5. End Function

    This is ok sometimes, if you're crafty in how you use said function. But for something that is going to get a lot of diverse use, you'd want to keep it simple and leave the error/msgbox handling to the button/form you're implimenting it in.





    Just some basic tips that I've picked up that I knew helped me out when I first started out, as well as now as I'm getting back into programming again. There are a lot of extremely smart, talented programmers on this site and I'm sure they have some even more helpful tips and suggestions (not to mention corrections to some of the things I may have goofed on in this topic). I would hope this topic would encourage some more helpful "getting started" tips for those who are struggling to get off the ground

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Useful Beginner Tips

    Re: Some general rules of thumb..

    Include using Option Explicit

  3. #3
    Lively Member dsuraj's Avatar
    Join Date
    Nov 2007
    Location
    Pune
    Posts
    70

    Re: Useful Beginner Tips

    Thanks dude . nice and useful info

  4. #4
    Addicted Member
    Join Date
    Nov 2007
    Location
    Belgium
    Posts
    154

    Re: Useful Beginner Tips

    This is really useful for me, I always make mistakes like that in my program and that takes a lot of time when I want to change a color or something like that.

  5. #5
    Lively Member
    Join Date
    Oct 2008
    Location
    I live in Iceland :)
    Posts
    107

    Re: Useful Beginner Tips

    Very Nice.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.