I'd like to freeze/lock my form element, when my radio button checked.
Is it solve with one database row? Should I use VBA scripting to do this?
A radio button is not for checking; that's what a CheckBox is for.
So, use a CheckBox and this code in the AfterUpdate event of this:
Private Sub YourCheckBox_AfterUpdate()
Dim Frozen As Boolean
Frozen = Nz(Me!YourCheckBox.Value)
Me!YourTextBox.Locked = Frozen
Me!YourTextBox.Enabled = Not Frozen
End Sub
Related
I was wondering how I can define a combo box in a form where the user can select an option from the existing list, and if he can't find the desired option, a "add a new option" can be accessed which converts the combo box to a text box for writing the new option?
You can put a button next to the combo, for an 'add option' which is triggered when the combo 'not in list' event is hit - and the button then would call a popup form - or in your suggestion, changes the visibility of an overlayed text box that allows for entry. I suggest a popup form, however, because then you can validate the input for length, content, etc - and then once the form or text box is filled in - exiting the text box, or submitting the form would save to your table for the drop down, refresh the combo box source query, and assign the value. I do this in VBA alot with popups.
Sorry my bad english :-(
You can directly use the "On Not in List" event. Look at the examples at
https://learn.microsoft.com/en-us/office/vba/api/Access.ComboBox.NotInList
This is the scenario: A combobox called cb and a textbox called tb. Visible property of tb is false. Combobox and Textbox have the same data source. When you type a value that is not in the combobox list, that value goes to the textbox; when you finished editing textbox, it is hidden again.
Private Sub cb_NotInList(NewData As String, Response As Integer)
' Suppress error message and undo changes.
Response = acDataErrContinue
Me!cb.Undo
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", vbOKCancel) = vbOK Then
Me!tb.Visible = True
Me!tb.Value = NewData
Me!tb.SetFocus
End If
End Sub
Private Sub tb_AfterUpdate()
' Hide again texbox
Me!tb.Visible = False
' Update combobox items
Me!cb.Requery
End Sub
Question...
How can I toggle the visibility of several other fields (checkboxes/textboxes) on form based upon the selection of a combobox item. The image below shows a listbox but either way, how do use vba code to turn on or off visibility of all the fields in the grey box. Basically, if the combobox selection is scheduled then visible=true. Else visible=false How can I code this???
Use combobox AfterUpdate event and probably form Current event as well. So build a procedure that can be called from both events, something like:
Sub Form_Current()
SetVisible
End Sub
Sub cbo1_AfterUpdate()
SetVisible
End Sub
Sub SetVisible()
Me.tbx1.Visible = Me.cbo1 = "scheduled"
Me.cbx1.Visible = Me.cbo1 = "scheduled"
End Sub
Alternative is to use Conditional Formatting for textboxes and comboboxes (sorry, not applicable to other controls) to Enable/Disable as well as set colors so appear not visible.
So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub
I am usgin Access 2010 and I have a form that has a sub form, let's call it sform. I want to print the parent form's active control When I hit a button in sform. I tried
debug.print Me.parent.ActiveControl
But it prints the name of the sub form's container control, while I wanted the one that was active before it. Is that even possible?
Thanks
The active control on the parent form would be the sub form. To figure out what control had the focus before the sub form you may have to do something like the following:
Add a text box to the form called txtFieldThatHadLastFocus or whatever and hide it.
Then in the On Lost Focus event of all your controls on the parent form add:
Private Sub txtMyField_LostFocus()
txtFieldThatHadLastFocus.Value = "txtMyField"
End Sub
Then within the sub form you can figure out what control had focus before the sub form via:
Forms!ParentFromName!txtFieldThatHadLastFocus.value
My problem is:
Main form has 9 command buttons and continuous subform. Click on some button open other form with text box for entering quantity and two command buttons. When click on the first button I want to use caption of pressed button from main form for query. How to get that button caption?
Thanks.
You have just clicked the button, you know the name of the control, so I do not quite see the problem, other than that it all sounds like quite a bad idea.
Private Sub cmdClickMe_Click()
Debug.Print Me.cmdClickMe.Caption
Debug.Print Screen.ActiveControl.Caption
End Sub