form loading the last state it was before hide() - vba

I have 2 forms. I want to open the second form in the last state it was left? I mean changes intact, textfields changed, choices made etc etc. I try using .show but it does load the form from its load sub which resets the form from the fresh state.
Can anyone guide me here? Thanks in advance

If you don't want the form to reload, don't destroy it.
In the main form, store a reference to the single instance of the second form.
Private m_Dialog As Form2
Private Sub Command1_Click()
If m_Dialog Is Nothing Then Set m_Dialog = New Form2
m_Dialog.Show
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Not m_Dialog Is Nothing Then Unload m_Dialog
Set m_Dialog = Nothing
End Sub
In the second form, use Hide() to close it.
Private Sub OKButton_Click()
Me.Hide
End Sub

Related

How to make a button greyed out FOREVER in MS Access

What I am trying to accomplish is this:
Make a button for End Time which greys out once it is clicked. When moving to a new or the next record the button appears not greyed out, but will grey out once clicked. However, when I move to the new record, the button previously clicked in the previous records appear once again. Below is my code for the on-click function of the button:
Private Sub Command28_Click()
txtEndTime = Time()
Command28.Enabled = False
End Sub
I have also written this code for the Form_Current function:
Private Sub Form_Current()
If Me.NewRecord Then
Me.Command28.Enabled = True
End If
End Sub
Any help would be greatly appreciated.
Try with:
Private Sub Form_Current()
Me!Command28.Enabled = Me.NewRecord
End Sub
And do rename it to something meaningful.

How to run vba macro after an access form has loaded?

When I call a sub in Form_Load, it gives me an error cause by Screen.ActiveForm. This is due to the form not being loaded yet.
What sub/function can I use to run a macro once the form has loaded.
I tried Form_Timer, it didn't do anything
Form_Activate produces same error
Form_after… they do not really seem to indicating anything after form load.
Here is my code for Form_Timer:
Private Sub Form_Timer()
call Module6.loadRecords
Me.TimerInterval = 500
End Sub
I was hoping that after 0.5 seconds that my form will be loaded and records will be display in the form controls.
Instead of depending on Screen.ActiveForm, you should simply pass the form reference to the function.
Private Sub Form_Load()
Call Module6.loadRecords(Me)
End Sub
and
Public Sub loadRecords(F As Access.Form)
If you really want to use Screen.ActiveForm, it works like this:
Private Sub Form_Load()
' 1 ms is enough to de-couple the events
Me.TimerInterval = 1
End Sub
Private Sub Form_Timer()
' Reset timer, always the first thing to do for single Timer events
Me.TimerInterval = 0
Call Module6.loadRecords
End Sub

VBA multiple option dialog box output

I created a user form with multiple options and now I want that the option the user selects is shown in a label under the button that calls the user form.I changed the caption in the text box under the button to resemble what should happen
However my options aren't working. Should I save the output in a global variable and then call it back to change the label and if so how do I do that? Or is it possible to just call the selection within the user form?
The code I was trying to run was this one to call the message box and then change the text box which is actually a label called "labelpage"
Private Sub CommandButton1_Click()
UserForm1.Show
If UserForm1.OptionButton1 = True Then LabelPage.Caption = "Company Restricted"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Strictly Confidential"
If UserForm1.OptionButton2 = True Then LabelPage.Caption = "Public Information (does not need to be marked)"
End Sub
I also had this for each button click just to close them after selection, within the user form code.
Private Sub OptionButton1_Click()
OptionButton1.Value = True
Unload Me
End Sub
Private Sub OptionButton2_Click()
OptionButton2.Value = True
Unload Me
End Sub
Private Sub OptionButton3_Click()
OptionButton3.Value = True
Unload Me
End Sub
Is there just a tiny mistake of syntax or something like that or is this just completely wrong? Thank you in advance for your help.
The issue is that you are unloading the UserForm, meaning the controls are not available to you. The solution is to just hide the UserForm:
Private Sub OptionButton1_Click()
Hide
End Sub
Private Sub OptionButton2_Click()
Hide
End Sub
Private Sub OptionButton3_Click()
Hide
End Sub

How to remove a Label in UserForm?

I have a UserForm with this function:
Public MyVariable As String
Private Sub UserForm_Initialize()
[...my code...]
End Sub
To call my Userform from a button i do:
Sub CallUserForm_Appro()
UserForm1.MyVariable = "Appro"
UserForm1.Show
End Sub
Sub CallUserForm_User()
UserForm1.MyVariable = "User"
UserForm1.Show
End Sub
My goal is to remove "Label1" if user click on button to call CallUserForm_Appro()
So, i tried in UserForm_Initialize() to do:
Public MyVariable As String
Private Sub UserForm_Initialize()
[...my code...]
If MyVariable = "Appro" Then
UserForm1.Controls.Remove "Label1"
End If
End Sub
I have no error but my Label1 is always visible.
This is how you set the visibility of the label to false:
UserForm1.label1.Visible = false
Then it should not be visible any more.
The `Initialize event occurs before the variable is set (because you can't access any property of the form without it being loaded first).
You should use the Activate event instead as long as the control is added at run time. If it's a design time control, you can't delete it, only hide it. Alternatively, you might only add it to the form if the variable is not set to "Appro"

Having an MS Office UserForm detect which subroutine called it

In a VBA project of mine I am/will be using a series of reasonably complex userforms, many of which are visually identical but have different subroutines attached to the buttons. As a result I'm not overly keen on the idea of duplicating them multiple times in order to get different functionality out of the same layout. Is it possible to have a userform detect which subroutine called it and use this in flow control? I would like to be able to do something like this:
Private Sub UserForm_Initialize()
If [the sub that called the userform is called "foo"] then
Call fooSub
else
Call barSub
End If
End Sub
My backup plan is to have the calling subroutine set a global variable flag and have the userform check that, but that seems like a rather crude and clumsy solution.
Thanks everyone,
Louis
You can use the tag property of the form. Load the form, set the property, then show the form:
Sub PassCallerToForm()
Load UserForm1
UserForm1.Tag = "foo"
UserForm1.Show
End Sub
Now that the property is set, you can determine what to do in the form:
Private Sub UserForm_Activate()
If Me.Tag = "foo" Then
Call fooSub
Else
Call barSub
End If
End Sub
You can also use public variables:
' in userform
Public Caller As String
Private Sub UserForm_Click()
MsgBox Caller
Caller = Now()
Me.Hide
End Sub
' in caller
Sub callUF()
Dim frm As New UserForm1
frm.Caller = "Test Caller"
frm.Show
MsgBox frm.Caller ' valid after Me.Hide
Set frm = Nothing
End Sub
Personally, I would not have one userform doing two disparate activities. The code would get hard to read pretty quickly, I think. Copying the layout of a userform is pretty trivial.
To copy a userform: Open a blank workbook. In the Project Explorer, drag the userform to the new workbook. Rename the userform in the new workbook. Now drag it back to the original workbook. Change the code in the userform copy.
If you absolutely don't want separate userforms, I recommend setting up a property of the userform. Userforms are just classes except they have a user interface component. In the userform module
Private mbIsFoo As Boolean
Public Property Let IsFoo(ByVal bIsFoo As Boolean): mbIsFoo = bIsFoo: End Property
Public Property Get IsFoo() As Boolean: IsFoo = mbIsFoo: End Property
Public Sub Initialize()
If Me.IsFoo Then
FooSub
Else
BarSub
End If
End Sub
I always write my own Initialize procedure. In a standard module:
Sub OpenForm()
Dim ufFooBar As UFooBar
Set ufFooBar = New UFooBar
ufFooBar.IsFoo = True
ufFooBar.Initialize
ufFooBar.Show
End Sub