-----Original Message-----
Here is the code, Dirk.
Dim rstRecordset As DAO.Recordset
Dim dbs As Database
Dim strIngrPrice As String
txtTxt.Value = ""
If cboComboBox <> "" Then
Set dbs = OpenDatabase("Database.mdb")
If DCount("*", "qryQuery") = 0 Then
Msg = MsgBox("...", vbCritical, "No data")
rstRecordset.Close
dbs.Close
Exit Sub
Else
Me.RecordSource = "qryQuery"
Set rstRecordset = Me.Recordset
strStr = rstRecordset![Cost]
txtTxt.Value = strIngrPrice
rstRecordset.Close
dbs.Close
End If
End If
Is that your real code, quoted exactly? I find it hard to believe your
real code has all those generic names: txtTxt, cboComboBox,
Database.mdb, qryQuery, strStr. Please, when you are asked to post your
code for remote debugging, unless there are real reasons of privacy and
security to argue against it, post the *real* code, not code that you've
modified just for the post. If you delete irrelevancies in the interest
of brevity, that's okay, but include a note that you've deleted
something.
There are lots of things wrong, or at least potentially wrong, with the
code you posted, but it's hard to be sure where the real problems lie
because I don't think that's your real code. Here are some possible
problem areas:
(1)
I take it this is a text box. Is it unbound or bound to a text field?
If not, the assignment will fail. If so, you may still want to use
txtTxt.Value = Null
to clear it, rather than assigning a zero-length string. In general,
Null is used in Access for unknown values, while the zero- length string
is used only for text values that are known not to exist. Note,
however, that this is unlikely to be the source of whatever error you
are experiencing.
(2)
If cboComboBox <> "" Then
Unless cboComboBox is bound to a text field that may contain a
zero-length string, this test will never be true. At a guess, you
should be testing like this:
If IsNull(cboComboBox) Then
(3)
Set dbs = OpenDatabase("Database.mdb")
Am I guessing right that "Database.mdb" is actually the current
database, the one in which this code is running? If so, you don't need
to use OpenDatabase to open it; it's already open. If you need it,
just use the CurrentDb function to get a reference to a copy of the
active database:
Set dbs = CurrentDb
HOWEVER, I don't see any reason in the posted code for you to be using a
database object at all. Quite likely this line should go away
altogether.
(4)
Set dbs = OpenDatabase("Database.mdb")
If DCount("*", "qryQuery") = 0 Then
Msg = MsgBox("...", vbCritical, "No data")
rstRecordset.Close
dbs.Close
Exit Sub
You don't need to open the database object dbs before calling DCount,
since that function uses its own database object internally.
At this point you haven't opened or set the recordset object
rstRecordset, so attempting to Close it will raise an error.
If you don't open the database object at this point (since you don't
need it in this branch of the logic) you shouldn't close it.
In the code as far as you've posted it, the Exit Sub statement is
unnecessary at this point. Since you haven't posted the complete code,
I can't tell whether you need it or not.
(5)
Me.RecordSource = "qryQuery"
Set rstRecordset = Me.Recordset
strStr = rstRecordset![Cost]
txtTxt.Value = strIngrPrice
rstRecordset.Close
dbs.Close
Your earlier description makes it sound like this is where you're findin
g a problem. I can't be sure what the problem is, though, until the
code is cleaned up. There's no declaration for "strStr", so I don't
know whether this is supposed to be a renaming of strIngrPrice or some
other variable. Are you trying to pick up the value from
rstRecordset![Cost] and put it in txtTxt? If so, why don't you just
bind that text box to the Cost field in the first place, since you're
making that query be the form's recordsource? Whatever you're doing
here, you're plainly going the long way around. But until I understand
what you're really trying to accomplish, I can't tell you how to fix it.
One thing I can tell you for sure: you must *not* close the form's
recordset! You didn't open it, and you shouldn't be closing it. So the
line ...
.... should be deleted. And if as I suspect you don't need a database
object at all, this line, too ...
.... should go.
That's about all I can tell right now, and I don't know if any of it is
going to solve your most immediate problem. If you'll tell me what
you're trying to accomplish, post your real code, and tell me what event
is executing it, I may be able to give better advice.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)
.