Connecting Tech Pros Worldwide Forums | Help | Site Map

Subform requery problem

Newbie
 
Join Date: Nov 2007
Posts: 12
#1: Dec 1 '07
Hi guys,
I recently rewrote onClick procedure for Calendar on my form (frmRota). It opens record from tblRotas where field rDate is equal the one selected on Calendar. If such a record doesnt exist I want it to be created. Before only way to create new record was to click on NewRecord button that I created using Access2003 button wizard. Calendar is not bound to any field in recordsource but on this form I have text box rDate that is bound to tblRota.rDate field which I update at the end of onClick procedure. Everything is ok when record exists - but when I try to create new record by clicking on Date that doesnt have corresponding record in my table (tblRotas) I get an error when populating combobox on subform attached to the frmRota form. Error is : "You entered an expression that has an invalid reference to the property Form/Report."
I distinguished the line that produces the error.
I know that my explanation might be a bit confusing I am pretty much novice in Access DB programming so please excuse me not the best vb code. Here is all the code attached to frmRota:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Calendar1_Click() 
  2. GBL_CurrRotaDate = Me.Calendar1
  3. GBL_CurrRotaDay = Get_DayFromDate(CStr(GBL_CurrRotaDate))
  4. Me.rWeekNo = Format(GBL_CurrRotaDate, "ww") - 8
  5. If Me.rWeekNo <= 0 Then
  6. Me.rWeekNo = Me.rWeekNo + 55
  7. End If
  8. STRSQL = "Select count(rDate) as Rcount from tblRotas where rDate = CDATE('" & GBL_CurrRotaDate & "')"
  9. Set Db = CurrentDb
  10. Set rs = Db.OpenRecordset(STRSQL)
  11. If Not rs.EOF Then
  12.  
  13. 'Check if Rota for that date already exists
  14. If rs!Rcount >= 1 Then ' Rota already exists
  15.  
  16. Me.rDate.SetFocus
  17. DoCmd.FindRecord Me.Calendar1, acEntire
  18. rs.Close
  19. Db.Close
  20. Exit Sub
  21. Else 'rota doesnt exist so create new record
  22. rs.Close
  23. Db.Close
  24. Me.Form.SetFocus
  25. DoCmd.GoToRecord , , acNewRec 
  26. If Not Me.Calendar1 Then 
  27. Me.rDate = Me.Calendar1 'calendar not bound to tblRotas.rDate so need to update rDate box which is bound to rDate field in tblRotas table
  28. GBL_CurrRotaDate = Me.Calendar1 'setting global rota date
  29. GBL_CurrRotaDay = Get_DayFromDate(CStr(GBL_CurrRotaDate))
  30. Me.rDate = Me.Calendar1
  31. Me.rWeekNo = Format(GBL_CurrRotaDate, "ww") - 8 ' where I leave financial year starts from March thats why -8 
  32. If Me.rWeekNo <= 0 Then 'if current data between January and March update week by adding 55 so week is not negative
  33. Me.rWeekNo = Me.rWeekNo + 55
  34. End If
  35. End If
  36. PopulateListsBoxes 'function at the end of frmRota module - within which I receive error when creating new record
  37.  
  38. End If
  39. End If
  40.  
  41. End Sub
  42.  
Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form_Current() 
  2. Dim rs As Recordset
  3. Dim Db As Database
  4. Dim STRSQL As String
  5.  
  6. If Not Me.rDate Then
  7. Me.Calendar1 = Me.rDate
  8. End If
  9.  
  10. If Not Me.CurrentRecord Then
  11. Me.Caption = "Create Rota No. " & Me.CurrentRecord
  12. End If
  13. If Not Len(Me.rDate) = 0 Then
  14. GBL_CurrRotaDate = Me.rDate
  15. GBL_CurrRotaDay = Get_DayFromDate(CStr(GBL_CurrRotaDate))
  16. PopulateListsBoxes
  17. End If
  18. End Sub
  19.  
Expand|Select|Wrap|Line Numbers
  1.  Private Sub sfrmRota_Enter() 
  2. If Me.rDate = 0 Then 'rDate is what bounds frmRota with sfrmRota so it cant be NULL or 0
  3. MsgBox "Please Select Date First"
  4. Me.Calendar1.SetFocus
  5. End If
  6. If IsNull(Me.rDate) Then 'rDate is what bounds frmRota with sfrmRota so it cant be NULL or 0
  7. MsgBox "Please Select Date First"
  8. Me.Calendar1.SetFocus
  9. Exit Sub
  10. End If
  11. End Sub
  12.  
Expand|Select|Wrap|Line Numbers
  1.  Public Function PopulateListsBoxes() 
  2. Set Db = CurrentDb
  3. CurrentDb.Execute ("Delete from tblAvailableEmployees")
  4. STRSQL = "Insert into tblAvailableEmployees (EmployeeID) Select employeeID from tblDaysOff where " & GBL_CurrRotaDay & _
  5. "=0" 'Selecting employees that are contracted on selected day
  6. CurrentDb.Execute (STRSQL)
  7. STRSQL = "Insert into tblAvailableEmployees (EmployeeID) Select employeeID from tblOT where otDate = #" & GBL_CurrRotaDate & "#;" 'Adding employees that agreed to do Overtime 
  8. CurrentDb.Execute (STRSQL)
  9. Me.sfrmRota.Controls.Item(2).Requery 'Thats the part that errors when creating new record. I am using this for combobox( item(2) on subform ) to requery tblAvailableEmployees for new data
  10.  
  11. End Function
  12.  

puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#2: Dec 1 '07

re: Subform requery problem


try this syntax:

Me!sfrmRota.Form!Item(2).Requery
Newbie
 
Join Date: Nov 2007
Posts: 12
#3: Dec 2 '07

re: Subform requery problem


Quote:

Originally Posted by puppydogbuddy

try this syntax:

Me!sfrmRota.Form!Item(2).Requery

Thanks but unfortuantely this doesn't help.
puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#4: Dec 2 '07

re: Subform requery problem


Quote:

Originally Posted by AdrianGawrys

Thanks but unfortuantely this doesn't help.

Please provide some details.......I am not sitting at your computer....what happens.....what is the error message ?
Newbie
 
Join Date: Nov 2007
Posts: 12
#5: Dec 5 '07

re: Subform requery problem


Quote:

Originally Posted by puppydogbuddy

Please provide some details.......I am not sitting at your computer....what happens.....what is the error message ?

Like I wrote in my first post - Error message is
"You entered an expression that has an invalid reference to the property Form/Report." and it comes up under line 9 of PopulateListsBoxes() function but only when condition from line 14 (If rs!Rcount >= 1 Then) of "Private Sub Calendar1_Click()" is False - meaning: there is no rota in tblRotas with date (rDate) specified by user (value of Calendar1) if condition from line 14 is met then PopulateListsBoxes doesn't fail - no error whatsoever.
Any ideas? I can't find a workaround to this problem.
puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#6: Dec 5 '07

re: Subform requery problem


Quote:

Originally Posted by AdrianGawrys

Like I wrote in my first post - Error message is
"You entered an expression that has an invalid reference to the property Form/Report." and it comes up under line 9 of PopulateListsBoxes() function but only when condition from line 14 (If rs!Rcount >= 1 Then) of "Private Sub Calendar1_Click()" is False - meaning: there is no rota in tblRotas with date (rDate) specified by user (value of Calendar1) if condition from line 14 is met then PopulateListsBoxes doesn't fail - no error whatsoever.
Any ideas? I can't find a workaround to this problem.


Well, if you want to fire the code below when you have an existing record:
=================================================
Me.sfrmRota.Controls.Item(2).Requery 'Thats the part that errors when creating new record. I am using this for combobox( item(2) on subform ) to requery tblAvailableEmployees for new data

Why can't you do it like this:
========================
If Not Me.NewRecord then
Me.sfrmRota.Controls.Item(2).Requery
Else
' what you want program to do if you have a new record
End If
Newbie
 
Join Date: Nov 2007
Posts: 12
#7: Dec 6 '07

re: Subform requery problem


Quote:

Originally Posted by puppydogbuddy

Well, if you want to fire the code below when you have an existing record:
=================================================
Me.sfrmRota.Controls.Item(2).Requery 'Thats the part that errors when creating new record. I am using this for combobox( item(2) on subform ) to requery tblAvailableEmployees for new data

Why can't you do it like this:
========================
If Not Me.NewRecord then
Me.sfrmRota.Controls.Item(2).Requery
Else
' what you want program to do if you have a new record
End If

Thanks a bunch, I feel like I am slowly moving in a good direction but the problem is that Me.NewRecord is set to 0 at this point for some reason. @puppydogbuddy would you be interested in looking into the database file itself? I can share them somewhere on public location and send you link if you are interested.
puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#8: Dec 6 '07

re: Subform requery problem


Quote:

Originally Posted by AdrianGawrys

Thanks a bunch, I feel like I am slowly moving in a good direction but the problem is that Me.NewRecord is set to 0 at this point for some reason. @puppydogbuddy would you be interested in looking into the database file itself? I can share them somewhere on public location and send you link if you are interested.

Adrian,
The code I gave you was : If <<<Not>>> Me.NewRecord then.

If you need me to look at the file, you can zip and email to the address on my VCard (with my profile), In order for me to look at your file, it has to be Access version 2000 or 1997(can convert to 2000).
puppydogbuddy's Avatar
Expert
 
Join Date: May 2007
Location: Florida
Posts: 1,915
#9: Dec 7 '07

re: Subform requery problem


Adrian,
Just noticed this. Could line 7 be your problem? it looks to me like line 7 is backwards. I think it should be: Me.rDate = Me.Calendar1
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current() 
  2. Dim rs As Recordset
  3. Dim Db As Database
  4. Dim STRSQL As String
  5.  
  6. If Not Me.rDate Then
  7. Me.Calendar1 = Me.rDate  '<<<<<<<<<<<<<<<<<<Problem???
  8. End If
Newbie
 
Join Date: Nov 2007
Posts: 12
#10: Dec 8 '07

re: Subform requery problem


Quote:

Originally Posted by puppydogbuddy

Adrian,
Just noticed this. Could line 7 be your problem? it looks to me like line 7 is backwards. I think it should be: Me.rDate = Me.Calendar1

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current() 
  2. Dim rs As Recordset
  3. Dim Db As Database
  4. Dim STRSQL As String
  5.  
  6. If Not Me.rDate Then
  7. Me.Calendar1 = Me.rDate  '<<<<<<<<<<<<<<<<<<Problem???
  8. End If

No it's not a problem actually - Calendar is not bound to rDate so if you open form with rota I need Calendar to show what day is the rota for. It's under Form_Current because before I had "move to next" and "move to previous" record selectors so also needed to show what date is the rota for.
Newbie
 
Join Date: Nov 2007
Posts: 12
#11: Dec 11 '07

re: Subform requery problem


Sorry for wasting your time but I manage to find a solution to my problem - I just removed the line where I execute PopulateListBoxes() function after creating new record and put it under AfterInsert() Sub of the form. That seem to fixed my problem - for some reason running my function right after creating new record didnt allow me to address subform's controls - like they haven't been created yet. Now everything seems to be ok.
Reply