Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Coding a pop up calendar!!!!

Status
Not open for further replies.
Aug 23, 2005
43
US
Hey guys. I am inserting a pop up calendar into a field of a form. I thought it was coded right...but when I tab over to the field on the actual form I get an error message.


The code:

Option Compare Database
Option Explicit

Private strGlobalUser As String

'This declares the MonthCalendar Class
Private mc As clsMonthCal

Private Sub Date_Trained_Enter()

Dim blRet As Boolean
Dim dtStart As Date, dtEnd As Date

dtStart = Nz(Me.Date_Trained, 0)
dtEnd = 0

blRet = ShowMonthCalendar(mc, dtStart, dtEnd)
If blRet = True Then
Me.Date_Trained = dtStart
Else
'Guess what you forgot to do?
'You forgot to select a date!
End If

End Sub

Private Sub Form_Load()

'Create an instance of our Class
Set mc = New clsMonthCal
'Set the hWndForm Property
mc.hWndForm = Me.hWnd

End

'Load proper source for the specific user
'Check if the User is OK, and if it's OK, create the record source
Dim DB As Database
Set DB = CodeDb
Dim rstUser As Recordset
Dim strCurrentUser As String

Set rstUser = DB.OpenRecordset("CurrentUser", dbOpenDynaset)

If rstUser.RecordCount <> 1 Then
'One record is OK
strCurrentUser = rstUser.Fields(0)
rstUser.Close

Select Case strCurrentUser
Case "GG"
Case "JB"
Case "JJ"
Case "JS"
Case "KK"
Case "TH"
Case Else
strCurrentUser = ""
End Select

If Trim(strCurrentUser) <> "" Then

Me.RecordSource = _
"SELECT " & Trim(strCurrentUser) & "Face2Face.CustomerID, " & Trim(strCurrentUser) & "Face2Face.[Current Prospect?], " & _
Trim(strCurrentUser) & "Face2Face.[Current Account?], " & Trim(strCurrentUser) & "Face2Face.[Boxed LW14], " & _
Trim(strCurrentUser) & "Face2Face.[Display LW14], " & Trim(strCurrentUser) & "Face2Face.[Boxed LW24], " & _
Trim(strCurrentUser) & "Face2Face.[Display LW24], " & Trim(strCurrentUser) & "Face2Face.[Boxed LW44], " & _
Trim(strCurrentUser) & "Face2Face.[Display LW44], " & Trim(strCurrentUser) & "Face2Face.[Boxed WTA], " & _
Trim(strCurrentUser) & "Face2Face.[Display WTA], " & Trim(strCurrentUser) & "Face2Face.[Boxed Cleaner], " & _
Trim(strCurrentUser) & "Face2Face.[Dipslay Cleaner], " & Trim(strCurrentUser) & "Face2Face.[Boxed Fragrance], " & _
Trim(strCurrentUser) & "Face2Face.[Display Fragrance], " & Trim(strCurrentUser) & "Face2Face.[Location and Placement of Display], " & _
Trim(strCurrentUser) & "Face2Face.[# of Associates Trained], " & Trim(strCurrentUser) & "Face2Face.[AOS Models & Quantity], " & _
Trim(strCurrentUser) & "Face2Face.Display, " & Trim(strCurrentUser) & "Face2Face.[Customer Training], " & _
Trim(strCurrentUser) & "Face2Face.[WTA & Cleaner], " & Trim(strCurrentUser) & "Face2Face.[Review RMA Policy], " & _
Trim(strCurrentUser) & "Face2Face.[Video Running], " & _
Trim(strCurrentUser) & "Face2Face.[Posters Up], " & _
Trim(strCurrentUser) & "Face2Face.Advertising, " & _
Trim(strCurrentUser) & "Face2Face.Other2, " & _
Trim(strCurrentUser) & "Face2Face.[Competitor Information], " & _
Trim(strCurrentUser) & "Face2Face.[Other Brands Sold], " & _
Trim(strCurrentUser) & "Face2Face.[Weber, Dyson, or Meile], " & _
Trim(strCurrentUser) & "Face2Face.[Week #], " & _
Trim(strCurrentUser) & "Face2Face.[Date Trained], " & _
Trim(strCurrentUser) & "Face2Face.[Time Trained] " & _
"FROM " & Trim(strCurrentUser) & "Face2Face;"
Else
'If it is the user without poermission
MsgBox ("You cannot run this macro. Please, contact application administrator")
Set rstUser = DB.OpenRecordset("CurrentUser", dbOpenDynaset)
Do While Not rstUser.EOF
rstUser.Delete
rstUser.MoveNext
Loop
rstUser.Close
DoCmd.Close acForm, "F2F", acSaveNo
DoCmd.OpenForm ("VentaInformationSystemLogin")
End If
Else
'It is possible that more then one user starts the application.
'In this case we do not know which user needs the own table
MsgBox ("Too many users. Please login again")
Do While Not rstUser.EOF
rstUser.Delete
rstUser.MoveNext
Loop
rstUser.Close
DoCmd.Close acForm, "F2F", acSaveNo
DoCmd.OpenForm ("VentaInformationSystemLogin")
End If

End Sub

Private Sub Form_Unload(Cancel As Integer)

'This is required in case user Closes Form with the
'Calendar still open. It also handles when the
'user closes the application with the Calendar still open.

If Not mc Is Nothing Then
If mc.IsCalendar Then
Cancel = 1
Exit Sub
End If


Set mc = Nothing
End If

End Sub
-----------------------------------------------------------

The error:

"Invalid MonthCalendar object!"

The MonthCalendar class instance you passed to this function is INVALID! YOu must instantiate the MonthCalendar Class object before you call this function. The code behind the sample Form shows you how to do this in the Form's Load event.

'This must appear here!
'Create an instance of our Class
Private Sub Form_Load()
Set mc=New clsMonthCal
'You must set the class hWndForm prop!!!
mc.hWndForm=Me.hWnd
-----------------------------------------------------------

Please help me out with this!!!

madetoheal13
 
My guess would be that you might have pasted in

Private Sub Form_Load()

'Create an instance of our Class
Set mc = New clsMonthCal
'Set the hWndForm Property
mc.hWndForm = Me.hWnd

End sub

And thus the onload event is not being trapped. Double check the onload event property to see that it has "[EventProcedure]" in it.
 
Hello!

Yes it does have [Event Procedure] in it. I have this same calendar in a different spot on the form and it works fine. In this case, the field is in a subform within the main form and in the form's Load event...there are 2 procedures running...as you can see above. again the error I am receiving is word for word in my first post. Thanks!

madetoheal13
 
Another wild guess. You declared mc as private to the Main form, but you call it from the sub form. You need to declare and instantiate in the sub form as well.

Private mc As clsMonthCa
 
Or maybe just make it public would work.
 
Hi! Thank you all for your help but I've tried all this. My first posting above shows the exact code from my subform. I have declared an instantiated it in the subform as well the main form. Again, there is a field in the main form which triggers the same popup calendar and it works perfectly. the code is written in the subform exactly the same, except if you notice above under the subForm_Load () after:

'Create an instance of our Class
Set mc = New clsMonthCal
'Set the hWndForm Property
mc.hWndForm = Me.hWnd


The code that follows opens user specific record sets as follows:

'Load proper source for the specific user
'Check if the User is OK, and if it's OK, create the record source
Dim DB As Database
Set DB = CodeDb
Dim rstUser As Recordset
Dim strCurrentUser As String

Set rstUser = DB.OpenRecordset("CurrentUser", dbOpenDynaset)

If rstUser.RecordCount <> 1 Then
'One record is OK
strCurrentUser = rstUser.Fields(0)
rstUser.Close

Select Case strCurrentUser
Case "GG"
Case "JB"
Case "JJ"
Case "JS"
Case "KK"
Case "TH"
Case Else
strCurrentUser = ""
End Select

If Trim(strCurrentUser) <> "" Then

Me.RecordSource = _
"SELECT " & Trim(strCurrentUser) & "Face2Face.CustomerID, " & Trim(strCurrentUser) & "Face2Face.[Current Prospect?], " & _
Trim(strCurrentUser) & "Face2Face.[Current Account?], " & Trim(strCurrentUser) & "Face2Face.[Boxed LW14], " & _
Trim(strCurrentUser) & "Face2Face.[Display LW14], " & Trim(strCurrentUser) & "Face2Face.[Boxed LW24], " & _
Trim(strCurrentUser) & "Face2Face.[Display LW24], " & Trim(strCurrentUser) & "Face2Face.[Boxed LW44], " & _
Trim(strCurrentUser) & "Face2Face.[Display LW44], " & Trim(strCurrentUser) & "Face2Face.[Boxed WTA], " & _
Trim(strCurrentUser) & "Face2Face.[Display WTA], " & Trim(strCurrentUser) & "Face2Face.[Boxed Cleaner], " & _
Trim(strCurrentUser) & "Face2Face.[Dipslay Cleaner], " & Trim(strCurrentUser) & "Face2Face.[Boxed Fragrance], " & _
Trim(strCurrentUser) & "Face2Face.[Display Fragrance], " & Trim(strCurrentUser) & "Face2Face.[Location and Placement of Display], " & _
Trim(strCurrentUser) & "Face2Face.[# of Associates Trained], " & Trim(strCurrentUser) & "Face2Face.[AOS Models & Quantity], " & _
Trim(strCurrentUser) & "Face2Face.Display, " & Trim(strCurrentUser) & "Face2Face.[Customer Training], " & _
Trim(strCurrentUser) & "Face2Face.[WTA & Cleaner], " & Trim(strCurrentUser) & "Face2Face.[Review RMA Policy], " & _
Trim(strCurrentUser) & "Face2Face.[Video Running], " & _
Trim(strCurrentUser) & "Face2Face.[Posters Up], " & _
Trim(strCurrentUser) & "Face2Face.Advertising, " & _
Trim(strCurrentUser) & "Face2Face.Other2, " & _
Trim(strCurrentUser) & "Face2Face.[Competitor Information], " & _
Trim(strCurrentUser) & "Face2Face.[Other Brands Sold], " & _
Trim(strCurrentUser) & "Face2Face.[Weber, Dyson, or Meile], " & _
Trim(strCurrentUser) & "Face2Face.[Week #], " & _
Trim(strCurrentUser) & "Face2Face.[Date Trained], " & _
Trim(strCurrentUser) & "Face2Face.[Time Trained] " & _
"FROM " & Trim(strCurrentUser) & "Face2Face;"
Else
'If it is the user without poermission
MsgBox ("You cannot run this macro. Please, contact application administrator")
Set rstUser = DB.OpenRecordset("CurrentUser", dbOpenDynaset)
Do While Not rstUser.EOF
rstUser.Delete
rstUser.MoveNext
Loop
rstUser.Close
DoCmd.Close acForm, "F2F", acSaveNo
DoCmd.OpenForm ("VentaInformationSystemLogin")
End If


...and so on. This is the only difference. Whereas on the main form the calendar is the only process to be run under the Form_Load() function....on the subform the calendar process is running along with another process to separate user specific information such as accounts. To be honest with you...my degree is in Finance not Computer Science...I built the majority of my company's database from books and forums like this...for the difficult code writing we contracted someone to write that...originally both calendars were working fine but after he added this code to pull out specific user info. I noticed the calendar was missing from the field so I pasted the calendar code from the main (properly working) form...to the subform and called the calendar to pop up from the date field in that form....the calendar window does popup on that field...it just pops up with the error from my first post. Thank you all again for your help.

madetoheal13
 
I am not sure what is the right code from the main and what is the code from the sub, but I do not think that this should work from your first post.
Code:
Private strGlobalUser As String

'This declares the MonthCalendar Class [b]But you do not 'instantiate it. You need a "As New clsMonthCal"[/b]
Private mc As clsMonthCal

Private Sub Date_Trained_Enter()

Dim blRet As Boolean
Dim dtStart As Date, dtEnd As Date

dtStart = Nz(Me.Date_Trained, 0)
dtEnd = 0
[b]This is never instantiated [/b]
blRet = ShowMonthCalendar(mc, dtStart, dtEnd)
 
Hey MajP!!!

Thanks! Worked like a charm...to think I've been searching for an answer for over a month...and all that was missing was the word "New"....Thank you soooo much.

madetoheal13
 
Personally, I never declare and instantiate in one line. You can search this site and see why this is not a great method.

Instead of:
Private mc As New clsMonthCal

I feel it is better to

Private mc as clsMonthCal
....
'Before you use it, instantiate it.
Set mc = new clsMonthCal
blRet = ShowMonthCalendar(mc, dtStart, dtEnd)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top