I have a problem with date and time picker when i select the date from dtpicker1 then txtdate1 gets the value but when i debug it shows txtdate1 = "" why please help

Private Sub DTPicker1_CloseUp()
txtdate1.Text = DTPicker1.Value
DTPicker1.Refresh
End Sub

Dani AI

Generated

Quick summary and root cause in one line: the picker/control value wasn't being safely read at the time the calculation ran, so a Null/empty date slipped into an arithmetic conversion and raised the runtime error. As observed, event timing matters; as warned, external code (refresh/clears) can interfere; fixed it by validating the control before using its value.

Clarification: "Invalid use of Null" (runtime error 94) happens when a Null/DBNull/empty Variant is used in a conversion or arithmetic (for example passing Null into CDate, DateDiff, +, -, etc.). It is not the same as division-by-zero. Always check the source value before converting or calculating.

Safe pattern (extract into a Date first, validate, then calculate):

' VB6-style example: validate strings then convert and compute
Dim d1 As Date
Dim d2 As Date

If Not IsDate(txtDate1.Text) Or Not IsDate(txtDate2.Text) Then
    MsgBox "Provide valid dates."
    Exit Sub
End If

d1 = CDate(txtDate1.Text)
d2 = CDate(txtDate2.Text)
MsgBox "Days between: " & DateDiff("d", d1, d2)

If reading the picker value directly (Variant-capable pickers), check for Null/IsDate first:

Dim v As Variant
v = DTPicker1.Value
If IsNull(v) Or Not IsDate(v) Then
    MsgBox "No date selected."
Else
    txtDate1.Text = Format$(CDate(v), "yyyy-MM-dd")
End If

Troubleshooting checklist (use alongside the snippets above): instrument events with Debug.Print to trace event order (CloseUp / Change / Validate / Click), search the form for any code that resets textboxes, prefer a post-selection event (ValueChanged/Change or the control's Validating/Validated flow) to commit the value, keep calculations working with typed Date variables (not raw strings), and use an unambiguous format like "yyyy-MM-dd" to avoid locale parsing problems.

Recommended Answers

All 9 Replies

Maybe it's because you place your code at the CloseUp event of the date picker, it seems that when the CloseUp event is being fired, the Value property still doesn't have the date selected...

why not change the event?, try to put it in LostFocus or Click events of the date picker...

I have tried both but didn't help me

Remove the refresh statement, it should solve your problem. Also, as PoisenedHeart pointed out, under your click event.

Also check your code that you are not calling a clean up of all text in your text boxes.

How should i call clean up method sorry i didn't understand??

No, you won't call clean up, just check if you do not have it in your code somewhere in the form -"Text1.Text = "" or Text1.Text = vbNullString".

This is also maybe why your textbox is cleared. If you do not have it, don't worry about it.:)

Its showing the date in textbox but when i click on calculate button it gives message invalid use of null but when i pass dates manually and click on calculate button then it perform calculation

The error "Invalid use of Null" means that you are trying to divide a number by zero.

Write some error trapping code just before the calculation -

If Text1.Text = vbNullString Then
msgbox "Add a number/date"
Else
'Do calculation
End If

Dear AndreRet i have resolved the problem by using validate event now it is working perfectly any way thanks for your kind and great help

My pleasure. Happy coding.:)

Have a wonderful new year.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.