Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2013
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to declare variables

    Whilst I have recorded and modified many macros I am still missing something in my knowledge of VB.

    In an access database I have the following code;

    Private Sub AddressLine1_AfterUpdate()
    Dim FirstName As String

    Me.FirstName = StrConv(Me.FirstName, vbProperCase)
    End Sub

    - Did I declare Firstname correctly?
    - What is the purpose of the "Me." part in front?
    - Can anyone recommend a good reference for VB?

    Thanks

  2. #2
    3 Star Lounger
    Join Date
    Apr 2002
    Location
    Redditch, Worcestershire, England
    Posts
    233
    Thanks
    0
    Thanked 22 Times in 21 Posts
    Quote Originally Posted by Gavin99 View Post
    Whilst I have recorded and modified many macros I am still missing something in my knowledge of VB.

    In an access database I have the following code;

    Private Sub AddressLine1_AfterUpdate()
    Dim FirstName As String

    Me.FirstName = StrConv(Me.FirstName, vbProperCase)
    End Sub

    - Did I declare Firstname correctly?
    - What is the purpose of the "Me." part in front?
    - Can anyone recommend a good reference for VB?

    Thanks
    Is this code you've inherited or something you've tried to write yourself?

    This code is a little mixed up! Declaring FirstName as you have is perfectly OK - it defines a local variable called FirstName which can hold text information ("strings").

    HOWEVER, you never use this variable in your procedure! The syntax Me.FirstName refers to the FirstName property of the Me object, and in Access "Me" is a shortcut referring to the form (or report) on which the code is acting.

    So your FirstName declaration is actually redundant here - the code is changing the FirstName object on the current form (I presume you have a TextBox or something called FirstName) to make it proper case.

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,435
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Gavin,

    You are not using VB but rather VBA (Visual Basic for Applications). VBA consists of two parts the control language (common to all applications, e.g. Access, Excel, Word, etc.) and the Object Model which is different for each application. Since your post is about Access here are some books I've found useful.

    Microsoft Access VBA Programming, Second Ed. for the Absolute Beginner, Michael Vine, ISBN: 1-59200-723-6 (includes CD)

    Access Database Design & Programming, Second Ed. Steven Roman, ISBN: 1-56592-626-9

    Note: these are both older books but the principles still apply although newer versions of Access will contain additional features. You may be able to find later editions online. HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

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