Results 1 to 3 of 3
  1. #1
    Join Date
    May 2009
    Posts
    104

    Unanswered: Global/Public Variables

    I need to be able to pass a variable from a procedure on one form to anther. Basically what this does is in our database there may be part numbers like 123-456/88", when we make files in windows i have to filter out the illegal characters in order to search and open a pdf or file with the same name. I want to be able to pass that variable "File" to another forms procedure. I have found a few examples on how to this but for some reason i'm just not getting it. Any help would be greatly appreciated.

    Code:
    Private Sub Command53_Click()
        Dim PartNumFilter
        Dim PartNum
        Dim PartDescFilter
        Dim PartDesc
    '-------------------------------------------------------
        PartNumFilter = Replace([Part Number], Chr(34), "_")
        PartNum = Replace(PartNumFilter, "/", "_")
        PartDescFilter = Replace([Part Number], Chr(34), "_")
        PartDesc = Replace(PartDescFilter, "/", "_")
    '-------------------------------------------------------
               If Len(Dir("\\TPIS01\engineering\drawing pdfs\drawings\" & PartNum & ".pdf")) > 0 Then   ' Check to see if file exists
                File = "\\TPIS01\engineering\drawing pdfs\drawings\" & PartNum & ".pdf"
                 Else
                    File = "\\TPIS01\engineering\drawing pdfs\drawings\" & PartDesc & ".pdf"
                        End If
        ShellExec File
    End Sub

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    1. If both procedures are in the same module, you can declare a private member variable in this module:
    Code:
    Option Compare Database
    Option Explicit
    '
    ' Declaration section of the module Form_Frm_wz_Menu.
    '
    Private m_booSelected As Boolean
    
    Private Sub Detail_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
        If m_booSelected = True Then Activate ""
        m_booSelected = False
        
    End Sub
    2. If the procedures are in different class modules (the module linked to a Form is a class module), you can declare a public variable in the module:
    Code:
    ' In the class module of the form Frm_wz_Menu.
    '
    Option Compare Database
    Option Explicit
    '
    ' Declaration section of the Module Form_Frm_wz_Menu
    '
    Public Selected As Boolean
    '
    ' ---
    '
    ' In the  Module Form_Frm_wz_Commands (the class module of another form).
    '
    Private Sub Command_ExitWizard_Click()
    
        If Forms("Frm_wz_Menu").Selected = False Then
            DoCmd.Close acForm, "Frm_wz_Menu", acSaveNo
            Application.Quit
        End If
        
    End Sub
    The danger with this technique is that the value of the variable Selected can be changed from everywhere in the application. To prevent this, you can add a public property (Selected) to the form Frm_wz_Menu, like this:
    Code:
    ' In the class module of the form Frm_wz_Menu.
    '
    Option Compare Database
    Option Explicit
    '
    ' Declaration section of the Module Form_Frm_wz_Menu
    '
    Private m_booSelected As Boolean
    '
    Public Property Get Selected() As Boolean
    
        Selected = m_booSelected
    
    End Property
    '
    ' ---
    '
    ' In the  Module Form_Frm_wz_Commands (the class module of another form).
    '
    Private Sub Command_ExitWizard_Click()
    
        If Forms("Frm_wz_Menu").Selected = False Then
            DoCmd.Close acForm, "Frm_wz_Menu", acSaveNo
            Application.Quit
        End If
        
    End Sub
    You can now read the value of the private member variable m_booSelected from everywhere in the application through the Selected public property of the form Frm_wz_Menu, but not change it. m_booSelected can only be modified by a procedure inside the module of the form Frm_wz_Menu.
    Have a nice day!

  3. #3
    Join Date
    May 2009
    Posts
    104
    I gave this a shot and can't seem to get this working. If i have a for named "FormA" with an on_click event with the following code.
    Code:
    Private Sub Command53_Click()
        Dim PartNumFilter
        Dim PartNum
        Dim PartDescFilter
        Dim PartDesc
    '-------------------------------------------------------
        PartNumFilter = Replace([Part Number], Chr(34), "_")
        PartNum = Replace(PartNumFilter, "/", "_")
        PartDescFilter = Replace([Part Number], Chr(34), "_")
        PartDesc = Replace(PartDescFilter, "/", "_")
    '-------------------------------------------------------
               If Len(Dir("\\TPIS01\engineering\drawing pdfs\drawings\" & PartNum & ".pdf")) > 0 Then   ' Check to see if file exists
                File = "\\TPIS01\engineering\drawing pdfs\drawings\" & PartNum & ".pdf"
                 Else
                    File = "\\TPIS01\engineering\drawing pdfs\drawings\" & PartDesc & ".pdf"
                        End If
        ShellExec File '<--- I want to pass this variable
    End Sub
    and this form has a button that opens up a popup form "FormB" with another on_click event that has the code
    Code:
    Private Sub Command32_Click()
    On Error GoTo Error_Handle
                            
    Dim Operator
    Dim cdoConfig
    Dim msgOne
    Dim Urg
    
    
     If [Frame47] = 1 Then
            Urg = "2"
                Else
                    Urg = "1"
                        End If
                        
    Operator = [Combo37]
    
    Dim sMsgBody As String
    
    Set cdoConfig = CreateObject("CDO.Configuration")
    With cdoConfig.Fields
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = 465
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.somewhere.com"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "some.local\notme"
    .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "123456"
    
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
    .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
    .Update
    End With
    
    
    Set msgOne = CreateObject("CDO.Message")
    Set msgOne.Configuration = cdoConfig
    msgOne.To = "ME"
    'msgOne.CC = "NotMe"
    msgOne.From = "StillNotMe"
    msgOne.Subject = "Drawing Revision Request From     " & [Combo37]
    
    msgOne.Fields.Item("urn:schemas:mailheader:X-Priority") = Urg ' For Outlook 2003 also
    msgOne.Fields.Item("urn:schemas:httpmail:importance") = Urg ' For Outlook Express
    msgOne.Fields.Update
    
        sMsgBody = "        " & vbCr & vbCr
        sMsgBody = sMsgBody & "Requesting Employee= " & Operator & RevDraw & vbCr
        
    '    msgOne.TextBody = "     Here is the link to the drawing revision request File   " & "     " & NewPath & File & "_Rev" & "_" & FO & "_" & ".pdf" & sMsgBody
             msgOne.TextBody = "     Here is the link to the drawing revision request File   " & "     " & DrawRev & sMsgBody
    msgOne.Send
                        
    
    Exit_Command32:
        MsgBox "Request Sent"
            DoCmd.Close acForm, "FormB"
    Exit Sub
    Error_Handle:
                MsgBox Err.Description
                    Resume Exit_Command32
    End Sub
    I have never tried to pass variables between forms in a event module before. My database has been running great for a couple of years so it's been a while since i have had to do any coding, so i'm a little slow. I apologize for my ignorance.

Posting Permissions

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