Hello I'm very new to vba in Access
I'm trying to get an array of the controls in my form through a button.
My end goal is to press a button and have it loop through the array of controls and ask if it is equal to -1 (the controls are Yes/No check boxes) and if it is, to open a corresponding form.
Anyone know how to do this?
Thanks in advance for the help!
Use code like this in the OnClick event of your button:
Const FormName As String = "CorrespondingFormName"
Dim Control As Control
For Each Control In Me.Controls
If Control.ControlType = acCheckBox Then
If Control.Value = True Then
Exit For
End If
End If
Next
If Not Control Is Nothing Then
' A checked checkbox was found.
DoCmd.OpenForm FormName
End If
Related
I have lots of controls on my userform and I want to disable all of them on load except for "login button" and "Shift Date" textbox, so i am using the code below:
Private Sub Form_Load()
Call GetValues
Me.WlcmLabel.Caption = "Hi " + GetUserName + " ! "
Dim ctrl As Control
For Each ctrl In Me.Controls
ctrl.Enabled = False
Next
Me.ShiftDate.Enabled = True
Me.LoginBtn.Enabled = True
Set ctrl = Nothing
End Sub
but this gives me an error on load saying "object does not support this property or method."
all the controls will get enabled as soon as the user clicks on login button.
What would be the mistake in my code ?
Please ask if any other information required.
Thank You !
Try locking the control instead. I have used the locked property below, but I use the tag property of each control to identify it as lockable. So the controls you don't want to lock will not get anything in the tag property, and therefore will not lock. For your application, you could flip the logic since you only want leave the two controls unlocked.
For Each ctlCurr In Me.Controls
If ctlCurr.Tag = "Lockable" Then
ctlCurr.Locked = True
End If
Next
You can't disable label control that's why that error is coming. You have to check the type of control.
For Each ctrl In Me.Controls
With ctrl
a = TypeName(ctrl)
Select Case .ControlType
Case acLabel
Case acEmptyCell
Case Else
ctrl.Enabled = False
End Select
End With
Next ctrl
Apply disable only when it's not label.
I would like to have a button to open a form and then run VBA code on that form. So either using Form_Load or and intermediate module. Does anyone know how to do this?
Thanks
Use OpenArgs.
First form:
DoCmd.OpenForm "SecondForm", OpenArgs:=Me.Name
Second form:
Private Sub Form_Load()
If Me.OpenArgs = "FirstForm" Then
' Stuff
End If
End Sub
Declare a module level variable in the second form:
Dim Prev As Form
In the On Load-event of the second form (this sets a reference to the first form):
Set Prev = Screen.ActiveForm
And in the On Close-event:
Set Prev = nothing
Now you can check the name of the previous form with:
If Prev.Name = "..." Then
... your actions
End If
Furthermore you can check any property or field from the first/previous form this way. Prev is acting like Me now.
Lets say the new form be opened from a number of forms in your application;
Can more than one of the calling forms be open at any one time?
If not, use this:
Private Sub Form_Load()
If isLoaded("Form1") then
Form1_InstructionSet
ElseIf isLoaded("Form2") then
Form2_InstructionSet
...
End If
End Sub
Private Sub Form1_InstructionSet
...
End Sub
etc.
If more than one of the calling forms can be open simultaneously, you should parameterise the new form, as per #Andre's answer above.
I have a form which has a button that has a click procedure.
Essentially, if a checkbox(chkIncludeIDs) is ticked then it has to open a form and make the property of a textbox on that form visible.
My current code for the click event is as follows:
Private Sub Update_pending_Click()
On Error GoTo Err_Update_pending_Click
Dim stDocName As String
Dim stLinkCriteria As String
If Me.chkIncludeIDs = 0 Then
stDocName = "Pending Queries Locked"
Else
stDocName = "Pending Queries Locked"
Forms![Pending Queries Locked]!ID.Visible = True
End If
DoCmd.OpenForm stDocName, , , stLinkCriteria
Exit_Update_pending_Click:
Exit Sub
Err_Update_pending_Click:
MsgBox Err.Description
Resume Exit_Update_pending_Click
End Sub
The form that I open is "Pending Queries Locked"
The textbox I need making visible is ID as it's default value is visible = false.
My code isn't working.
please name your controls without spaces like "form_pending_queries_locked", query_my_query, txt_id, lbl_id for many good reasons.
when you access a control's property you need to specify which property you are accessing.
e.g
If Me.chkIncludeIDs.value <> 0 Then
regarding your question. You need to open the form first before accessing its controls. so that would be logically: open the form, access the control, change its property.
in code it would be
Dim txt_id As Textbox
DoCmd.OpenForm "frm_myform"
Set txt_id = Forms![frm_myform]!txt_id
txt_id.visible = False
or alternatively: you can send a parameter via OpenArg and access it via on form_load event which will then change local controls properties..
I'm trying to disable a button when it is clicked:
Private Sub submitButton_Click()
Me.submitButton.Enabled = False
If dataPath = "" Or skuPath = "" Then
MsgBox "Please enter spreadsheet locations."
Else
Call importExcelData(dataPath, "data")
Call importExcelData(skuPath, "skus")
If validate = True Then
Call generateReports
End If
End If
End Sub
I'm getting a runtime error: "You can't disable a control while it has the focus".
Do you know a way around this?
Thanks!
You must first set the focus to a different control.
That said, if you really have no other control to send the focus to (which I'd find hard to believe), there is a "workaround." You can add a command button and set its TabStop property to False and its Transparent property to True and set the focus to that control.
I'm trying to modify existing code to add a popup box. This popup box is another form, and when a user clicks on a button on this form, I want a textbox on the base form to be populated. After that the popup disappears and then I need to access that value from the textbox on the base form.
The sequence should be:
Base form button click calls modal popup
Click in button on popup saves value to base form's textbox, then returns control.
Base form then uses this value to do something.
Base Form
Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
MsgBox Me.TheAnswer 'This line gives a null error
End Sub
Popup Form
Private Sub btnRolls_Click()
'Me.Tag = 1
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Me.Visible = False
End Sub
Private Sub btnSheets_Click()
'Me.Tag = 4
Forms!("ReceiptDetail_sfrm").TheAnswer = 4
Me.Visible = False
End Sub
You can do it that way, you probably need something like:
Private Sub btnRolls_Click()
Forms!ReceiptDetail_sfrm!TheAnswer = 1
Forms!ReceiptDetail_sfrm.Refresh
Me.Visible = False
End Sub
I've done what you're doing many times. I'm not at work to check my code right now for an example, but it's doable.
What I tend to do is hide the popup, then read from it:
Main Form:
Private Sub base()
DoCmd.OpenForm "PaperType", , , , , acDialog
'code waits for modal hide here
Me!TheAnswer = Forms("PaperType").SomethingOnThatFormThatStoresTheValue
DoCmd.Close acForm, "PaperType", acSaveNo
MsgBox Me.TheAnswer
End Sub
Popup
Private Sub btnRolls_Click
'may be hidden control
Me.SomethingOnThatFormThatStoresTheValue = TheValueToRead
Me.Visible = False
End Sub
Make sure that either A) Your popup can't be closed from the form itself, or B) Your calling code will handle the error of trying to read something that's no longer loaded (or both).
Apparently the correct answer is: "Don't do it this way!". Instead of trying to pass data between the forms, which is a pain, I made a table in Access which only has 1 field in 1 record. Then I store the value in there using DoCmd.RunSQL, and retrieve it from the other form using DLOOKUP().