Hide/unhide combobox based on a text box - ms-access

I have a form in access that works in this way:
User select an ID of a product
VBA code searches in the database and fill some textboxes in the form.
For example:
User select ID 1
Textbox 1 is filled with: fruit
My problem is, i have a combobox with options to select like "banana" or "apple" or "grape". This combobox is visible at the form, but i want it to be invisible and only appears if the textbox 1 is filled with "fruit".
In the design view, i set the combobox's visibility to "no", so it does not appear anymore in the form. Then, i wrote this code:
Private Sub textbox1_AfterUpdate()
If Me.textbox1 = "fruit" Then
Me.LabelOfTheCombobox.Visible = True
Me.Combobox.Visible = True
Else
Me.LabelOfTheCombobox.Visible = False
Me.Combobox.Visible = False
End if
End Sub
But it doesnt work, when the textbox is filled with "fruit", the combobox remains invisible.
Can you help me fix this code?

Everything looks OK. From what you have said, that putting "fruit" in the textbox doesn't cause the combobox to display, then my guess would be that the If-test is failing. Perhaps you could try trimming both sides of the textbox before the comparison. You should also pay attention to the case. Try forcing the case of the textbox to lower case to match the string constant.

Related

how to create a combo box in access which can be converted to text box to enable writing if the user needs to add a new option to the existings?

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

Access - Show values from subform in texboxes

I have a form "frm_Results" with subform "subfrm_shv_Results" and three textboxes ("txt_Number", "txt_Name", 'txt_Surname").
I can show values of selected record in Debug.Print or MsgBox:
Debug.Print Form_frm_Results.subfrm_shv_Results.Form!Number.Value
Debug.Print Form_frm_Results.subfrm_shv_Results.Form!Name.Value
Debug.Print Form_frm_Results.subfrm_shv_Results.Form!Surname.Value
But it is possible to show values of "Number", "Name" and "Surname" in textboxes after click on record in subform "subfrm_shv_Results"?
Yes, you can do this, with a query as the contained object, you need to refer to the controls property, for example, you can set the control source of a textbox to :
= [Forms]![frm_Results]![subfrm_shv_Results].form.controls(0)
Similarly, with a continuous form or such like :
= [Forms]![frm_Results]![subfrm_shv_Results].Form!Surname
The result is not editable, but you can get around this by setting the value in VBA.
Make sure you use the name of the subform control, not the the name of the form contained.

autopopulating text boxes from combo boxes

I have created a form to input new work orders in access. I want the text boxes in my form to populate from the selection in the combo box.
The combo box lists serial number for equipment, and once a serial number is selected, I want CompanyName, PurchDate, LastMaintenanceDate, & ModelNo to populate.
I saw an example of dlookup and tried this expression on the Company Name textbox:
=DLookUp("[CompanyName]","SerialNoInfo","[SerialNoInfo].SerialNo= " & Str([Combo341].[Text]))
I keep getting an error #Type or #Error in the box depending on what I do. When I try to build a code for the event on update, nothing happens at all. I have no idea what I am doing wrong.
You can alternatively use :
Private Sub COMBOBOX_Change()
me.TEXTBOXNAME.Text = me.COMBOBOX.Column(X)
End Sub
Where TEXTBOXNAME is the the textbox you want to fill
Where COMBOBOXis the the combobox from which you want to get data
and X is the number of column with data (starting from 0 )

MS Access de-select listbox after running macro/query

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

combobox cascade effect not working for different rows

I have two comboboxes on a subform.The first combobox is used to populate the second combobox. These are placed in the detail section of the form. I want them to work this way:when I select any value from the first combobox, I want the second combobox of the same row to get populated by relevant value.
As of now, I have tried to implement this and as I select any value from the first combobox of row 1 I see the second combobox of the same row gets populated but as I go on selecting values from the first set of comboboxes I see that the values in the second set of the comboboxes above changing or becoming null.
Here's the code:
The 1st combobox is cboRCMTask:
Private Sub cboRCMTask_AfterUpdate()
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
Me.cboRCMTaskOptions.Requery
End Sub
cboRCMTaskOptions is the second combobox.
The form_current event:
Private Sub Form_Current()
Me.cboRCMTask.RowSource = "SELECT ID, RCMTask FROM tblRCMTask;"
If IsNull(txtRCM_ID) Then
Me.cboRCMTask = Me.cboRCMTask.ItemData(0)
End If
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
If IsNull(txtRCMOption_ID) Then
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
End If
End Sub
From your description, you are using a continuous form. While it looks like a continuous form has many rows, from the point of view of coding you can consider it to have just one row, the current row. I suspect that the control source for combo 2 is a hidden, numeric column in the combo, when you change the row source for the combo, the visible row can no longer be found, so it cannot be displayed. You will either have to provide a pop-up form for editing, or a textbox to store the value for the form and a slightly dodgy combo to edit that value. You can do a little to improve the appearance with conditional formatting.
in your first piece of code, shift the code to the on_click event. I am not sure what you are trying to achieve with the reference to ItemData but I think it is unnecessary, comment out that line.
Similarly the third to last line in the Form_current event, replace with a requery.