I have been working on a report that I feel is nearing completion, however, my code doesn't seem to be working quite how I expected it...
I have a report for last month's program usage by low-income residents, i.e. each usage for food, shelter, clothing, etc. That report has a crosstab query as it's record source. The problem is that there is 1 program that may or may not be present each time the query is run. I have created an unbound textbox for that 1 program and IF the program is used last month, then I'd like for it's control source to change to the newly included field "Back to School", because in normal circumstances, it's not so until needed the control source is unbound. Here is the code I'm using in the report's OnOpen event and the function to check if the field exists:
CODE
Private Sub Report_Open(Cancel As Integer)
If FieldExists("qryMSR_CT_LastFullMonthEthnicitiesXPrograms", "Back to School") = True Then
' Field exists
Me.txtB2School.ControlSource = "Back to School"
Else
' Field does not exist
Me.txtB2School.Visible = False
Me.BacktoSchool_Label.Visible = False
End If
End Sub
Public Function FieldExists(queryName As String, fieldName As String) As Boolean
On Error GoTo FieldExists_Exit
Dim fld As Field
For Each fld In CurrentDb.QueryDefs(queryName).Fields
If fld.Name = fieldName Then
FieldExists = True
Exit For 'Or Exit Function
End If
Next
FieldExists_Exit:
End Function
If FieldExists("qryMSR_CT_LastFullMonthEthnicitiesXPrograms", "Back to School") = True Then
' Field exists
Me.txtB2School.ControlSource = "Back to School"
Else
' Field does not exist
Me.txtB2School.Visible = False
Me.BacktoSchool_Label.Visible = False
End If
End Sub
Public Function FieldExists(queryName As String, fieldName As String) As Boolean
On Error GoTo FieldExists_Exit
Dim fld As Field
For Each fld In CurrentDb.QueryDefs(queryName).Fields
If fld.Name = fieldName Then
FieldExists = True
Exit For 'Or Exit Function
End If
Next
FieldExists_Exit:
End Function
My problem is even if I change the field to a field that I know is in there....the code still returns a false and I don't know why....can anyone help?
Thank you for any and all help you can provide!!!