Hi Guys,
Is it possible to format a text box based on the selection in a combo box. What I need is when “Date” is selected in the combo box the text box needs to be formatted as a date field.
Any help would be great.
Richard
Whilst you can easily change border colour and other properties, how does that address the question about formatting the text box according to the datatype selected in the combo?
Hi Isladogs,
The text box is used in a search query, I have 5 options in the combo box and the search results are returned on the text value in the text box. One of the options is “Date” so I would like to give the option of selecting the date from a date picker.
I don't see how a combo with all those different value types could ever contain a date. Any table field providing all those different "types" would have to be text and I suspect that value list members can only be text, therefore there can be no date data type here, and if the "dates" are dynamic you can't refer to them explicitly. What you could do is validate that the selected item is a date...
If IsDate(combo) Then
If you look on isladogs website he has an excellent sample datepicker. You could always add code that if the combobox = date, it would then open the datepicker or make it visible if embedded in the form.
I would fix the design flaw in the table. Tables are not spreadsheets. EVERY row for a column should contain the same data type. So if sometimes you have a date and sometimes you have a string, you should use two columns. The only way the table will even allow this mish-mash is if you define the data type as text which of course is what is causing your formatting problem.
I think we need clarification from the OP. They way I'm reading his/her question is that s\he has an unbound textbox which supplies a variable to a query and wants a datepicker should date be chosen in the combobox for ease of data entry.
Agree that more context/examples needed. Seems a bit of mishmash that could very well be attributed to table design, business process(es) involved. Date, Part No., Description, Supplied By & Serial No. seem separate, but related, and may indicate "which process" to do next????? more info please.
I tested this and it works as CJ_London describes. I am not sure of the confusion, because the request seemed pretty logical to me. You have a combo box that lists fields to filter and a textbox to enter the search value. If you change the format to a date format the date picker will be available, change it back and it goes away. I would use a custom date picker for many reasons, but this does work as the OP asked.
Code:
Private Sub cmboField_AfterUpdate()
If Not IsNull(Me.cmboField) Then
If Me.cmboField = "Date Hired" Then
Me.txtSearch.Format = "Short Date"
Else
Me.txtSearch.Format = ""
End If
End If
End Sub
Private Sub CmdSearch_Click()
Dim fltr As String
If Not IsNull(Me.cmboField) And Not IsNull(Me.txtSearch) Then
Select Case Me.cmboField
Case "Date Hired"
fltr = "[DateHired] = #" & Me.txtSearch & "#"
Case "Company"
fltr = "[Company] = '" & Me.txtSearch & "'"
Case "First Name"
fltr = "[First Name] = '" & Me.txtSearch & "'"
End Select
Me.Filter = fltr
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
Me.Refresh
End Sub
Private Sub CmdClear_Click()
Me.Filter = ""
Me.FilterOn = False
End Sub
I tested this and it works as CJ_London describes. I am not sure of the confusion, because the request seemed pretty logical to me. You have a combo box that lists fields to filter and a textbox to enter the search value. If you change the format to a date format the date picker will be available, change it back and it goes away. I would use a custom date picker for many reasons, but this does work as the OP asked.
Code:
Private Sub cmboField_AfterUpdate()
If Not IsNull(Me.cmboField) Then
If Me.cmboField = "Date Hired" Then
Me.txtSearch.Format = "Short Date"
Else
Me.txtSearch.Format = ""
End If
End If
End Sub
Private Sub CmdSearch_Click()
Dim fltr As String
If Not IsNull(Me.cmboField) And Not IsNull(Me.txtSearch) Then
Select Case Me.cmboField
Case "Date Hired"
fltr = "[DateHired] = #" & Me.txtSearch & "#"
Case "Company"
fltr = "[Company] = '" & Me.txtSearch & "'"
Case "First Name"
fltr = "[First Name] = '" & Me.txtSearch & "'"
End Select
Me.Filter = fltr
Me.FilterOn = True
Else
Me.Filter = ""
Me.FilterOn = False
End If
Me.Refresh
End Sub
Private Sub CmdClear_Click()
Me.Filter = ""
Me.FilterOn = False
End Sub