Results 1 to 4 of 4
  1. #1
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99

    Need some help on the variable scope

    Hi All,
    I have a swith board with many command buttons to open the other forms to enter data.
    And I want the add this function to the switch board. When I enter the RepairID, and click on certain button, it will automatically find the relevant form and open. The code is like this:
    Code:
    Private Sub cmdInitialCheck_Click()
    Dim strPartNumber As String
    Dim strATA As String
    Dim strFormName As String
     
    strPartNumber = DLookup("PartNumber", "tblRepairID", "[RepairID]='" & Me.RepairID.Value & "'")
     
    strATA = DLookup("ATA", "tblATA", "[PartNumber]='" & strPartNumber & "'")
    strFormName = "frm" & strATA & "InitialCheck"
     
    DoCmd.OpenForm strFormName
     
    End Sub
    Here is my question, how can I make the strPartNumber and strATA can be used on all the button of the form? So that I dont need to type it again and again in the code of other command buttons. I think I'm just lazy...
    I searched and found maybe the private variable is what I need, but I failed to get it work.
    Thanks in advanced...

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Did you try declaring those variables in the header section of the module?
    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
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    Code:
    Option Compare Database
    Option Explicit
    Private mstrPartNumber As String
    Private mstrATA As String
    mstrPartNumber = DLookup("PartNumber", "tblRepairID", "[RepairID]='" & Me.RepairID.Value & "'")
    mstrATA = DLookup("ATA", "tblATA", "[PartNumber]='" & mstrPartNumber & "'")
     
    
    Private Sub cmdInitialCheck_Click()
        
        Dim strFormName As String
        
        
        strFormName = "frm" & mstrATA & "InitialCheck"
        Debug.Print strFormName
        DoCmd.OpenForm strFormName
        
    End Sub
    Tried this but failed...

  4. #4
    blueraincoat is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Mar 2011
    Location
    Shanghai,CHN
    Posts
    99
    I found what's wrong...
    I should put the following code in the click event

    mstrPartNumber = DLookup("PartNumber", "tblRepairID", "[RepairID]='" & Me.RepairID.Value & "'")
    mstrATA = DLookup("ATA", "tblATA", "[PartNumber]='" & mstrPartNumber & "'")

    This seems like a silly thread...
    And it did not save much work actually - -

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

Similar Threads

  1. Variable Filter
    By breana in forum Queries
    Replies: 5
    Last Post: 03-11-2011, 11:08 AM
  2. Text Box to Variable
    By B-dub in forum Programming
    Replies: 1
    Last Post: 01-21-2011, 09:30 PM
  3. Report with variable name
    By KickPuncher in forum Reports
    Replies: 13
    Last Post: 12-30-2010, 11:04 AM
  4. Replies: 4
    Last Post: 08-05-2010, 01:26 PM
  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