HasData variable not defined

B

Beth

I have recently discovered HasData thanks to this site
and find it handy to prevent opening a form that is
called via a command button on a MAIN form.

When my MAIN form is bound all is good with my world. Now
I am trying to do the same with an unbound MAIN form. The
error is "variable not defined". My code seems correct,
so I infer that the problem is the unbound form. This
unbound form is merely a holder for many buttons to open
other forms and reports. Am I correct and what do I do
about it?

My code is below. Any MVP like to help me out with this?
I am not a professional, nor do I play one on TV.

Thanks,
Beth

*****************************************************
(this code is attached to the OnClick of a command button)
If HasData(CLid) Then
Dim stDocName As String
stDocName = "frmCL"
DoCmd.OpenForm stDocName
Else
MsgBox "There is no data."
End If
****************************************************
Function HasData(CLid) As Boolean
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim SQL As String
Set db = CurrentDb()

SQL = "SELECT [qryCL].CLid FROM [qryCL] WHERE [CLid] ='"
& [CLid] & "';"

Set rst = db.OpenRecordset(SQL)

If rst.EOF And rst.BOF Then
HasData = False
Else
HasData = True
End If
End Function
****************************************************
 
B

Beth

Kevin, thanks for the input.

I did as you suggested confident that would not solve my
issue and it did not. It does further indicate that my
problem is my MAIN form is UNBOUND. So...given the MAIN
form is UNBOUND, it will always return as undefined. The
control [CLid] is not in the recordsource as there is no
recordsource.

I tried moving the coding to the form being called. When
a record exists, the form opens properly. When no record
exists, I get the message "2427 You entered an expression
that has no value." No surprise, there is no record. But
I know that, I just want it to NOT open the form.

Any other ideas? Anyone?

Thanks,
Beth
-----Original Message-----
I get this error routinely and it is because I either
declare the variable after I attempt to use it or I have
not declared it at all. The first problem I corrected by
putting all variable declarations at the start of my
procedure. The second problem should be easy to correct
because the VBA editor should (in debug mode) highlight
the offending variable. Make sure you have the following
at the top of your procedure:

On Error Goto 0 (that's zero)

If you have this in there, the VBA editor will allow you
to "Debug" the code and will highlight the offending
variable.

Hope this helps!

Kevin

P.S. I am not an MVP, but I try!
-----Original Message-----
I have recently discovered HasData thanks to this site
and find it handy to prevent opening a form that is
called via a command button on a MAIN form.

When my MAIN form is bound all is good with my world. Now
I am trying to do the same with an unbound MAIN form. The
error is "variable not defined". My code seems correct,
so I infer that the problem is the unbound form. This
unbound form is merely a holder for many buttons to open
other forms and reports. Am I correct and what do I do
about it?

My code is below. Any MVP like to help me out with this?
I am not a professional, nor do I play one on TV.

Thanks,
Beth

*****************************************************
(this code is attached to the OnClick of a command button)
If HasData(CLid) Then
Dim stDocName As String
stDocName = "frmCL"
DoCmd.OpenForm stDocName
Else
MsgBox "There is no data."
End If
****************************************************
Function HasData(CLid) As Boolean
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim SQL As String
Set db = CurrentDb()

SQL = "SELECT [qryCL].CLid FROM [qryCL] WHERE [CLid] ='"
& [CLid] & "';"

Set rst = db.OpenRecordset(SQL)

If rst.EOF And rst.BOF Then
HasData = False
Else
HasData = True
End If
End Function
****************************************************
.
.
 

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

Similar Threads


Top