Solved Standard Modules and Global Variables (1 Viewer)

silversun

Registered User.
Local time
Today, 02:56
Joined
Dec 28, 2012
Messages
204
Hi all,
I've used a global variable declared in a standard module. It takes its value from form A and I want to display that value in a control in form B.
Although this is my first experience working with modules and global variables but it surprisingly worked the way it was supposed to.
Unfortunately after some other works when I came back and retest the form that uses the global variable I realized it is not working properly.
I can see the desired value is assigned to my global variable using AfterUpdate event of an Option Group in form A but it is not shown in form B even once I re-query my form B.
Please help me to understand the issue and fix it. Some parts of my VBA codes are given here.

Code:
Private Sub opt_AmPm_F_AfterUpdate()
hh_F = Me.opt_hours_F
mm_F = Me.opt_minutes_F
ap_F = Me.opt_AmPm_F
If (ap_F = 1) Then
    ampm_F = "AM"
    Else
    ampm_F = "PM"
    End If
If (mm_F = 0) Then
    mm_F = "00"
    Else
    mm_F = mm_F
    End If
myvarF = hh_F & ":" & mm_F & " " & ampm_F
Me.txt_temp_F = myvarF
Forms!frm_everything.Requery
End Sub
And this is my standard module, I called it as its default name: "Module1"
Code:
Option Compare Database
Global myvarF As String
The default value of the control in form B (frm_everything) is myvarF in its property sheet.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:56
Joined
Oct 29, 2018
Messages
21,357
Hi. Global variables are only visible to VBA. Using the name of a global variable as a Control Source won't work. You'll need to create a function to return the value of the global variable and use the function on your form.
 

vba_php

Forum Troll
Local time
Today, 04:56
Joined
Oct 6, 2019
Messages
2,884
Using the name of a global variable as a Control Source won't work.
he's not doing that. he's assigning the global to the box one time. that's not the same as binding it, is it? doesn't sound right.

silver,

why are you putting a string variable into a form box? why not just use a field value with the format of TIME? looks to me that you're doing more work than necessary here. ;)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:56
Joined
May 7, 2009
Messages
19,169
Code:
Private Sub opt_AmPm_F_AfterUpdate()
hh_F = Me.opt_hours_F
mm_F = Me.opt_minutes_F
ap_F = Me.opt_AmPm_F
If (ap_F = 1) Then
    ampm_F = "AM"
    Else
    ampm_F = "PM"
    End If
If (mm_F = 0) Then
    mm_F = "00"
    Else
    mm_F = mm_F
    End If
myvarF = hh_F & ":" & mm_F & " " & ampm_F
Me.txt_temp_F = myvarF
' change theTextbox with the correct name of textbox on frm_everything
[Forms]![frm_everything]![theTextbox].DefaultValue = """" & myvarF & """"
[Forms]![frm_everything].Form.Recalc
End Sub
on the 3rd line before "End Sub", change theTextbox to correct name of textbox on frm_everything.
 

silversun

Registered User.
Local time
Today, 02:56
Joined
Dec 28, 2012
Messages
204
Code:
Private Sub opt_AmPm_F_AfterUpdate()
hh_F = Me.opt_hours_F
mm_F = Me.opt_minutes_F
ap_F = Me.opt_AmPm_F
If (ap_F = 1) Then
    ampm_F = "AM"
    Else
    ampm_F = "PM"
    End If
If (mm_F = 0) Then
    mm_F = "00"
    Else
    mm_F = mm_F
    End If
myvarF = hh_F & ":" & mm_F & " " & ampm_F
Me.txt_temp_F = myvarF
' change theTextbox with the correct name of textbox on frm_everything
[Forms]![frm_everything]![theTextbox].DefaultValue = """" & myvarF & """"
[Forms]![frm_everything].Form.Recalc
End Sub
on the 3rd line before "End Sub", change theTextbox to correct name of textbox on frm_everything.
Hi,
Thanks for your help. It actually works fine now. You are the best.
Now I have to questions here.
I was told this is not the safest way to use global variables and it can make unexpected issues later on and very hard to do error handling, why is that if it is correct. Is there a better way to send the value of a textbox over another textbox in a different form, in the same project?
What is the difference between .Requery and .Recalc in terms of functionality?
 

silversun

Registered User.
Local time
Today, 02:56
Joined
Dec 28, 2012
Messages
204
he's not doing that. he's assigning the global to the box one time. that's not the same as binding it, is it? doesn't sound right.

silver,

why are you putting a string variable into a form box? why not just use a field value with the format of TIME? looks to me that you're doing more work than necessary here. ;)
I am trying to display the selected values in a textbox (in form A) locally in AfterUpdate event and simultaneously sending to another textbox in form B. I am using string format because I have numbers and characters at the same place, txt_temp_F <<== 09:45 PM
With the modifications "Arnelgp" suggested my code is working fine now. Thank you for your time anyway.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:56
Joined
Oct 29, 2018
Messages
21,357
I am trying to display the selected values in a textbox (in form A) locally in AfterUpdate event and simultaneously sending to another textbox in form B. I am using string format because I have numbers and characters at the same place, txt_temp_F <<== 09:45 PM
With the modifications "Arnelgp" suggested my code is working fine now. Thank you for your time anyway.
Hi. Glad to hear you got it sorted out. Good luck with your project.
 

silversun

Registered User.
Local time
Today, 02:56
Joined
Dec 28, 2012
Messages
204
Hi. Glad to hear you got it sorted out. Good luck with your project.
Thanks.
Do I need to indicate the solution as "Accepted Answer" or something like that? I don't see that button here to click on.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:56
Joined
Oct 29, 2018
Messages
21,357
Thanks.
Do I need to indicate the solution as "Accepted Answer" or something like that? I don't see that button here to click on.
Hi. Good question. I know the forum allows for you to mark the thread as solved, but not sure about designating a specific post as the accepted answer. So, at the very least, you could mark this whole thread as "solved."
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:56
Joined
Feb 28, 2001
Messages
26,996
I was told this is not the safest way to use global variables and it can make unexpected issues later on and very hard to do error handling,

Using public variables in a general module works fine UNLESS you have an untrapped error and get the dreaded popup that offers you the chance to "END" "DEBUG" "RESET" - because unless you are very adroit, you will end up doing something that resets your code and when that happens, data in public variables will be lost. You might look into TempVars (which you can look up online).

As to RECALC vs. REQUERY, the former only recomputes formulas that are in a .ControlSource while the latter actually re-evaluates everything associated with a .RecordSource, re-feeds bound values, and may include a RECALC.
 

Users who are viewing this thread

Top Bottom