Open Form Code

G

Guest

I have a textbox named txtShowMonth. I would for when I open the form the
user is prompted to enter a date in short date format (1/1/2005) and then
have that date entered into thetxtShowMonth textbox. This is what I have so
far:

Private Sub Form_Open(Cancel As Integer)

Dim strShowMonth As String
Dim blnDone As Boolean

Do
strShowMonth = InputBox("What month would you like to enter?")

Select Case True
Case (Len(strShowMonth) = 0)
Cancel = True
blnDone = True
End Select

Loop Until blnDone

If Cancel <> True Then
Me.txtShowMonth = strShowMonth
Me.Requery
End If

End Sub

Don't laugh too hard, I have almost no experience with coding. But I do know
I do want an alternative (such as a pop-up calendar) and I want to the code
to be close to the one above (copied to from a very similar form that entered
years instead of dates).

I have repeatedly tried to figure this out, so help would be greatly
appreciated!
 
N

Nikos Yannacopoulos

Tandy,

Make a new form called frmCalendar, and place a calendar control on it
(Insert > ActiveX Controls, select Calendar Control X.0); its name
should default to Calendar0. Then add a command button, and click cancel
as soon as the button wizard comes up; its name should default to
Command1. If the names are different, you'll have to change accordingly
in the code below. Same goes for the original form's name, which I will
assume to be frmMainForm, and the target date control on the form,
assumed named txtShowDate.

Select the command button, change its Caption property (tab Format) to
OK or something, then got to tab Events, put the cursor in the On Click
event and click on the little button with the ellipsis sign that appears
on the right; select Code Builder. You will be taken to a VBA editor
screen, and these two lines of code will be there:

Private Sub Command1_Click()

End Sub

Paste the following in between:

Forms![frmMainForm]!txtShowDate = Me.Calendar0
DoCmd.Close acForm, Me.Name

Go back to form design, save and close. Now open the main form in design
view, select control txtShowDate, go to its On Enter property on tab
Events and do as before; you should get the lines:

Private Sub txtShowDate_Enter()

End Sub

Paste the following code in between:

DoCmd.OpenForm "frmCalendar"
If IsDate(Me.txtShowDate) Then
Forms![frmCalendar]!Calendar0 = Me.txtShowDate
Else
Forms![frmCalendar]!Calendar0 = Date
End If

You no longer need the separate input or controls for year, month, day.
Upon entering the control, the calendar form pops up; if there was
already a date in txtShowDate, the calendar will open to that date;
otherwise, it will default in the current date. When the user selects a
date with the mouse and clicks OK, the calendar form will pass the
selected date to txtShowDate, and close. Simple, intuitive, very little
code and no invalid entries.

HTH,
Nikos
 

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