PRB: No Current Record Error In VB When Database is EmptyID: Q106494
|
If a data control and text box are both bound to an empty table in a
database, clicking the data control arrows gives this error:
No Current Record
The program does not know the table is empty until the automatic record
update occurs. That automatic record update occurs when you click the data
control arrow or you enter text in the text box and then execute an AddNew
or Edit method.
When the table is not empty the automatic update is a nice feature, but
when the table is empty the automatic update causes the no current record
error. The error message occurs because the underlying recordset contains
no records.
You must execute AddNew to create a current record before doing anything
that causes an automatic record update.
To work around this behavior, execute the AddNew method on an empty database table before allowing the user to click the data control or enter text into the bound text box control. For example, set the Enabled property for the text control and data control to False at the beginning of the program. Then you can force the user to click a command button that executes the AddNew method before enabling the text and data controls.
This behavior is by design. This design is under review and will be
considered for enhancement in a future release.
Control Name Property New Value NOTE
------------------------------------------------------------------------
Data1 DatabaseName C:\VB\TEST1.MDB Empty MDB created above.
Data1 RecordSource tbl1 Table name.
Text1 DataSource Data1 Name of data control.
Text1 DataField Fld1 Field name.
Command1 Caption "Press for AddNew"
Sub Command1_Click ()
data1.Recordset.AddNew
text1.SetFocus
End Sub
Data1.enabled = True
Text1.enabled = True
Dim db As database
Dim ds As dynaset
Sub Command1_Click ()
Set db = OpenDatabase("TEST1.MDB")
Set ds = db.CreateDynaset("tbl1")
' Execute the following line to work around problem:
' ds.AddNew
If IsNull(ds(0)) Then 'The No Current Record error occurs here
Print "No entry"
Else
Print ds(0)
End If
End Sub
Additional information can be found in the Visual Basic Help menu. The no
current record error is described in the Visual Basic Help topic "Data
Access error messages." Here is that description:
No current record. Error 3021.
This error occurs following the unsuccessful application of one of the FindFirst, FindLast, FindNext, FindPrevious Methods or the Seek Method, or when the underlying recordset contains no records. Move to or select a record, and then try the operation again.
The AddNew method clears the copy buffer in preparation for creating a new record in a Table or Dynaset. AddNew sets all fields in the copy buffer to Null and makes it the current record. After putting data in the record, you can use the Update method to add the record to the recordset. Update is automatically invoked with a data control if an Edit or AddNew operation is pending when you use one of the Find or Move methods.
Additional query words: 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 14, 1999