Results 1 to 5 of 5
  1. #1
    mahmud1180 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    20

    Form information as a variable in VBA


    I am struggling to use the form data as a variable in my VBA code. Like if I put in the form a text box that has 'variable1 1', in VBA code it will appear as variable1=1. Is it possible? I have tried several ways like bang operator but it did not work. Thanks in advance.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Certainly can reference form controls. A textbox named tbxQuantity could be referenced like:

    If Me.tbxQuantity > 0 Then
    ...
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mahmud1180 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    20
    Thank you June for your quick reply.

    Private Sub Command2_Click()




    If Me.Contract1 = 1 Then
    Firstcontract = 5
    Else
    Firstcontract = 0
    End If

    MsgBox (Firstcontract)
    End Sub

    I put it like that but when I ran the command button it says : compile error( Method and data are not found).

    Do I need to put anything else inside the subroutine or change the properties of the text in the form?

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Maybe you can declare a variable to hold the value of your result. Here I use type double but you may be able to use another data type, string, integer, long, etc.

    Dim dblFirstcontract as Double

    If Me.Contract1 = 1 Then
    FdblFirstcontract= 5
    Else
    dblFirstcontract = 0
    End If

    MsgBox (dblFirstcontract)

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Which line causes the error?

    Even without explicit declaration of variable, the code should work. Every code module should have the following two lines in module header:

    Option Compare Database
    Option Explicit

    The second line will require all variables to be declared.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 05-02-2014, 09:27 AM
  2. Replies: 3
    Last Post: 05-28-2013, 12:53 PM
  3. Replies: 2
    Last Post: 03-15-2013, 12:49 PM
  4. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  5. Refering to variable form names inside a variable
    By redpetfran in forum Programming
    Replies: 2
    Last Post: 05-21-2010, 01:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums