Recordset Error

T

Tim

Hi folks,

I need a help on my following code:

Private Sub Form_Current()
Dim dbCurrent As Database
Dim rst As Recordset

Set dbCurrent = CurrentDb
Set rst = dbCurrent.OpenRecordset("select field1 from
table1")

Me.Text0.Value = rst.Fields("field1").Value

End Sub

The code is working fine if the recordset is not null. If
the recordset is null, it will give me an error message.
I want me.text0.value=null when recordset is null.

Could anyone show me how to do it?

Thanks in advance.

Tim.
 
D

Dirk Goldgar

Tim said:
Hi folks,

I need a help on my following code:

Private Sub Form_Current()
Dim dbCurrent As Database
Dim rst As Recordset

Set dbCurrent = CurrentDb
Set rst = dbCurrent.OpenRecordset("select field1 from
table1")

Me.Text0.Value = rst.Fields("field1").Value

End Sub

The code is working fine if the recordset is not null. If
the recordset is null, it will give me an error message.
I want me.text0.value=null when recordset is null.

Could anyone show me how to do it?

Thanks in advance.

Tim.

A recordset can't be Null, but I suspect you mean that it contains no
records. You can check for that easily enough like this:

Set rst = dbCurrent.OpenRecordset( _
"select field1 from table1")

With rst
If .EOF Then
Me.Text0.Value = Null
Else
Me.Text0.Value = rst.Fields("field1").Value
End If
.Close '*** you should close the recordset when you're done.
End With
 
T

Tim

Dirk,

It works great.

Thanks.

Tim.
-----Original Message-----


A recordset can't be Null, but I suspect you mean that it contains no
records. You can check for that easily enough like this:

Set rst = dbCurrent.OpenRecordset( _
"select field1 from table1")

With rst
If .EOF Then
Me.Text0.Value = Null
Else
Me.Text0.Value = rst.Fields("field1").Value
End If
.Close '*** you should close the recordset when you're done.
End With

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


.
 

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