How to Set Temporary Value (2 Viewers)

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
Hi, I need to change my Macro to VBA code, but I'm wrong somewhere. So I have this Macro on my command button "cmd_1":

UG Added Code Tags
Code:
SetTempVar
     Name: temVarLaboratory
     Expression: [LaboratoryID]
OpenForm
     FormName: sub_Indicators
     Where condition = [LaboratoryID]=[TempVars]![temVarLaboratory]
 
'That is the Macro code who I use and it works well, but I need to write in VBA code, 
'because I want to add a few more things:
    If Me.txt_HM= 0 Then
        MsgBox "Please tick checkbox"
        Me.txt_HM.SetFocus
        Cancel = True
        Exit Sub
    End If
 
Last edited by a moderator:

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
I tend to use the format
Code:
TrempVars("temVarLaboratory") = [LaboratoryID]

but you do noy need a Tempvar here and could justuse the [LaboratoryID] directly

Code:
DoCmd.OpenForm,,,"[LaboratoryID]=" & Me.LaboratoryID

use whatever you were using to set the tempvar if it is within scope.

HTH
 

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
but you do noy need a Tempvar here and could justuse the [LaboratoryID] directly

Code:
DoCmd.OpenForm,,,"[LaboratoryID]=" & Me.LaboratoryID
I have tried with DoCmd.OpenForm "sub_Indicators", , , "[LaboratoryID] = " & Me.LaboratoryID, but when the form is open LaboratoryID is set to "0".

I wonder why in the loaded form "sub_Indicators" Property sheet - Filter [LaboratoryID]= 652 (this is the correct number in first form), but on form view the field LaboratoryID is "0" ?
 
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
Well assigning it to another variable is not going to fix that problem.
Walk through the code in the debug window with F8 after setting a breakpoint.

Sounds like you are not referencing the correct control?

HTH
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:56
Joined
Feb 19, 2002
Messages
42,981
No, if it were defined to open in Add mode, it would ignore the where argument so it would open to a new record rather than an existing record.
 

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
I can't fix the problem the code:

Code:
DoCmd.OpenForm "sub_Indicators", , , "[LaboratoryID] = " & Me.LaboratoryID
In Immediate windows show me this error: Compile error: Variable not yet created in this context.
Аny ideas for the reason?
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
LaboratoryID is not a control on your form?
 

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
No, it is not a control. LaboratoryID on the second form is set by

Form_Load()

DoCmd.GoToRecord , , acNewRec.
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
I'm lost on what is first form and what is second form:confused:

You said eearlier that syntax was giving you zero when opening the form. You told Pat that you were not opening the form in Add mode, yet now you say the load has DoCmd.GoToRecord , , acNewRec, which as far as i know is doing the same thing, just a different method.?

Best upload your db or cut down version and explain the steps needed to replicate your problem, else we could be back and forth for days.
If it cannot be opened in 2007, then other will need to assist.
 

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
I'm lost on what is first form and what is second form


You said eearlier that syntax was giving you zero when opening the form. You told Pat that you were not opening the form in Add mode, yet now you say the load has DoCmd.GoToRecord , , acNewRec, which as far as i know is doing the same thing, just a different method.?

Best upload your db or cut down version and explain the steps needed to replicate your problem, else we could be back and forth for days.
If it cannot be opened in 2007, then other will need to assist
It turned out to be quite confusing with all these explanations. I'll upload db
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
Best remove the Solved marker as well, as it has not been yet and might get more attention then.?
 

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
Here is my database. I put two buttons on the form"subformNonStandard" cmdHm and cmdMB. When I click on the cmdMB I have Macro to open form "Form_mb" to specific record , and it opens the form "Form_mb" correct.
When I try to open the form "Form_hm" by click on the button cmdHm with this code
DoCmd.OpenForm "Form_hm", , , "[LControlsID]=" & Me.LControlsID
it opens the form, but not to specific recort.
What I miss in my code?
 

Attachments

  • DB_v1.accdb
    1.5 MB · Views: 117
Last edited:

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
I get unrecognised database format, so I cannot help with that, sorry. :(
 

Gasman

Enthusiastic Amateur
Local time
Today, 00:56
Joined
Sep 21, 2011
Messages
14,048
Nope, still get the error. I have a very old version of Access.
 

tihmir

Registered User.
Local time
Yesterday, 17:56
Joined
May 1, 2018
Messages
257
Nope, still get the error. I have a very old version of Access.
The db is created on Access 2019.
OK, not a problem. :)

Thanks anyway for your time. Maybe someone else will help with my problem.
 

Users who are viewing this thread

Top Bottom