Assigning current value to default for date ComboBox not working?

L

LotusEater

I have a button for "go to next record" on a form. I want to assign the
current value chosen in a date combo box as the default value before moving
to the next record.

I am using the code below:
Private Sub cmdNextRecord_Click()
Me!combo_date.DefaultValue = Me!combo_date.Value
On Error GoTo Err_cmdNextRecord_Click
DoCmd.GoToRecord , , acNext
Exit_cmdNextRecord_Click:
Exit Sub
Err_cmdNextRecord_Click:
MsgBox Err.Description
Resume Exit_cmdNextRecord_Click
End Sub

It will assign the date of 12/30/1899 for the next record. I put a
breakpoint in the VBA code to see what the values were after this step.

Me!combo_date.Value
Value = 7/30/2009
Type = Variant/Date

Me!combo_date.DefaultValue
Value = "7/30/2009"
Type = Variant/String

I have tried wrapping the statement in numerous date commands such as
DateValue() and CDate() to keep it from converting to a string.

Any ideas?
 
C

Clifford Bass

Hi,

It will always convert it to a string since DefaultValue is a string
variable. So, what is happening when you assign the value 7/30/2009 is that
it interprets that a as an calculation 7 divided by 30 divided by 2009. This
is less than 1 so you get the date for the numeric value of 0, which is
12/30/1899. So, what do you do? You tell it that you are providing a date.
Try enclosing the value with the pound signs that indicate a date/time:

Me!combo_date.DefaultValue = "#" & Me!combo_date.Value & "#"

Clifford Bass
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top