Change text box format based on combo box selection (1 Viewer)

Sticky99

Registered User.
Local time
Today, 03:31
Joined
Nov 9, 2019
Messages
61
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.

Thanks
 

isladogs

MVP / VIP
Local time
Today, 11:31
Joined
Jan 14, 2017
Messages
18,209
Can you explain the context as there's probably a better solution
 

ontopofmalvern

Registered User.
Local time
Today, 11:31
Joined
Mar 24, 2017
Messages
64
Have an "on change" event. something llike this

If me.cmbx = "Date" Then
Me!txtBox.BorderColor = Red
Else if Me!.cmbx = "Name" Then
Me!txtBox.BorderColor = Blue
Else if....

If the there are lots of variables you maybe better off with a' Select case' type thing.


These two links might help

Hope this helps
Richard
 

isladogs

MVP / VIP
Local time
Today, 11:31
Joined
Jan 14, 2017
Messages
18,209
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?
 

Sticky99

Registered User.
Local time
Today, 03:31
Joined
Nov 9, 2019
Messages
61
Can you explain the context as there's probably a better solution
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.
 

Sticky99

Registered User.
Local time
Today, 03:31
Joined
Nov 9, 2019
Messages
61
Part No., Description, Supplied By & Serial No.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 11:31
Joined
Feb 19, 2013
Messages
16,607
perhaps something like this in the combo after update event

if combo="Date" then textbox.format="short date" else textbox.format=""
 

Sticky99

Registered User.
Local time
Today, 03:31
Joined
Nov 9, 2019
Messages
61
perhaps something like this in the combo after update event

if combo="Date" then textbox.format="short date" else textbox.format=""
Thnank CJ_London, I'll give it a try and let you know.
 

Micron

AWF VIP
Local time
Today, 06:31
Joined
Oct 20, 2018
Messages
3,478
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
 

moke123

AWF VIP
Local time
Today, 06:31
Joined
Jan 11, 2013
Messages
3,912
so I would like to give the option of selecting the date from a date picker.
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:31
Joined
Feb 19, 2002
Messages
43,223
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.
 

moke123

AWF VIP
Local time
Today, 06:31
Joined
Jan 11, 2013
Messages
3,912
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.
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:31
Joined
Jan 23, 2006
Messages
15,379
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.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 06:31
Joined
May 21, 2018
Messages
8,525
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
 

Attachments

  • ChangeFormat.zip
    139 KB · Views: 77

Sticky99

Registered User.
Local time
Today, 03:31
Joined
Nov 9, 2019
Messages
61
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
Thanks MajP, works perfectly!

Thank you :)
 

Users who are viewing this thread

Top Bottom