Microsoft KB Archive/106494

= PRB: No Current Record Error In VB When Database is Empty =

Article ID: 106494

Article Last Modified on 10/28/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q106494



SYMPTOMS
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

This error also occurs if you enter text into the text box, and execute the AddNew method or the Edit method while the database table is empty.

The program has difficulty recovering because of the recurring "No Current Record" errors. The On Error statement fails to trap this error.



CAUSE
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.



WORKAROUND
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.



STATUS
This behavior is by design. This design is under review and will be considered for enhancement in a future release.

How to Create an Empty Microsoft Access Database
Before using Example 1 or 2 shown below, create an empty database by following these steps in Visual Basic:


 * 1) Open the Data Manager program by choosing it from the Window menu in Visual Basic or by running DATAMGR.EXE from the Windows File Manager.
 * 2) In the Data Manager, choose New Database from the File menu and select Access 1.1.
 * 3) Click the New button and enter tbl1 for the table name.
 * 4) Click the Design button. Then click the Add button. Enter fld1 for the Field Name and select Integer for the Field Type.
 * 5) Save this Microsoft Access database with the name TEST1.MDB. Close the Data Manager.

Example One: Steps to Reproduce Behavior using a Data Control
 Start a new project in Visual Basic. Form1 is created by default. Create the empty Microsoft Access database (TEST1.MDB) as described above.  Add the following controls and set the following properties:   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"

  Add the following code in the Command1 Click event procedure: Sub Command1_Click data1.Recordset.AddNew text1.SetFocus End Sub

 From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program.</li> Either of the following will give the no current record error:

<ol style="list-style-type: lower-alpha;"> Click any of the arrow buttons on the data control.</li> Enter text into the text box, and then click the command button.</li></ol>

Notice that if you rerun the program, press the Command1 button first, and then enter some text into the Text1 text box, you can then click any of the arrow buttons on the data control without getting the error.</li></ol>

Example One Workaround
To work around this behavior, set the Enabled property for the text and data controls to False at design time or in the Load procedure for the form. Then add the following to the top of the Command1_Click procedure before the AddNew:

Data1.enabled = True Text1.enabled = True

This prevents the user from automatically updating an empty database, thus avoiding the no current record error.

Example Two: Steps to Reproduce Behavior Using Object Variables
<ol> Start a new project in Visual Basic. Form1 is created by default.</li> Create the empty Microsoft Access database (TEST1.MDB) as described above.</li> Add a command button. Enter "Press to Test" as its Caption property.</li>  Add the following code in the general Declarations section of Form1: Dim db As database Dim ds As dynaset

</li>  Add the following code in the Command1 Click event procedure: 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

</li> From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program.</li></ol>

To correct the error, add the line ds.AddNew shown in a comment above.

<div class="references_section">