Problem with Code

benc

Registered User.
Local time
Today, 17:56
Joined
Dec 31, 2001
Messages
57
I have created a Global Variable and function as below:

Code:
Option Compare Database

Dim gvactype As String
Dim gvchecktype As String

Public Function Returngvactype() As String
    Returngvactype = gvactype
End Function

Public Function Returngvactype() As String
    Returngvchecktype = gvchecktype
End Function

In the Query i have written:

SELECT [tblA/CParts].[Part ID], [tblA/CParts].[Part Number], [tblA/CParts].Description, [tblA/CParts].Qty, [tblA/CParts].[JIC Ver], [tblA/CParts].[JIC Number], [tblA/CParts].Discontinued, [tblA/CParts].Notes
FROM [tblA/CParts]
WHERE [tbla/cParts].[Aircraft Type] = Returngvactype()
WHERE [tbla/cParts].[Check Type] = Returngvchecktype()

And to put a value in from a form into the vairable i have written:

Code:
Option Compare Database

Private Sub searchparts_Click()

    DoCmd.OpenForm "FrmPartslistresults"
    gvactype = Me.frmactype
    gvchecktype = Me.frmchecktype
    
    MsgBox gvactype ''i have created the msgbox to makesure the values have entered
    MsgBox gvchecktype
        
End Sub

I am sure that there is an easier way to do this or that my code is wrong but cant seem to find the answer in past posts can anyone help.

Thanks
 
Last edited by a moderator:
where does it error out?... what are your results?
 
If the form opening show's the results of the query then it should, of course, be opened after you have assigned the values to the global variables...
 
I rearranged the code and it now loads the forms. When it produces the msgbox of the gv it returns the correct values however when i call the returnfunction so i can use the variables in the queries there are no values. is there something wrong with my module.

Option Compare Database

Dim gvactype As String
Dim gvchecktype As String

Public Function Returngvactype() As String
Returngvactype = gvactype
End Function

Public Function Returngvchecktype() As String
Returngvchecktype = gvchecktype
End Function

Thanks for your help in advance
 
To return global variables like this you have to have code running all the time. Such as open all your forms before you close the previous one. If at a time code stops the variable values disapear. The way around this is to make a class module and pass each instance thru to the next form by reference. The memory is then stored and that reference can be used from each form that gets passed that data.
 
I have transfered the code across the a class module but still the returnfunction has no values. I have attatched my db so you may have a look. frmsearch is the form that loads the values into the variables. It opens frmpartslistresults which is based on query, QryPartslist. Thanks for having a look.
 

Attachments

FloBob said:
To return global variables like this you have to have code running all the time. Such as open all your forms before you close the previous one. If at a time code stops the variable values disapear.

As long as an error doesn't occur then a global variable will remain in memory from the moment the database is opened until the database is closed. I don't know what you are talking about with respect to forms; they shouldn't be a factor.
 
i thought so, so why doesnt my returnfunction take across the gv or am i going about calling them into the wuery all wrong with a return function
 

Users who are viewing this thread

Back
Top Bottom