Kenny smith
Kenny smith

Reputation: 27

In vba how can we pass values from Forms to Module

I'm having trouble to use Module to call the values that user input in the Form. Can anyone help?

  **code in Module** 
    Option Explicit
    Private Sub OpenQueryFile()
    
    Dim fso As Scripting.FileSystemObject
    Dim ts As Scripting.TextStream
    Dim QueryString As String
    Dim QueryFilePath As String
    Dim pcode As String
    Dim date1 As String



Dim PID As Range
Set PID = UserForm1.Range("text1")

Dim PID1 As Range
Set PID1 = UserForm1.Range("text2")

enter image description here

   **code in Form**
Private Sub CommandButton1_Click()
Range("text1") = pcodebox.Value
Range("text2") = edatebox.Value
Call OpenQueryFile
End Sub

Upvotes: 0

Views: 38

Answers (1)

ArcherBird
ArcherBird

Reputation: 2134

I'm a little confused about what you are doing in your code... but I'm going to answer the question you've asked, which is about extracting the values from controls on a userform.

You should create an instance of the form to work with in the module. In the form object, you can expose a property to retrieve the value you want.

Assuming you are trying to get the values from pcodebox and edatebox textbox controls... and assuming they are in a UserForm that is named UserForm1 ...

Option Explicit
Private Sub OpenQueryFile()
    
    Dim frm as UserForm1
    Dim pcode As String
    Dim date1 As String

    Set frm = New UserForm1
    frm.Show

    '// user inputs stuff into form
    '// making a lot of assumptions here about how your form works...

    pcode = frm.pcodebox.Value
    date1 = frm.edatebox.Value

    '//do stuff with your text values....

End Sub

Upvotes: 1

Related Questions