PDA

Click to See Complete Forum and Search --> : addnew on form load event


christophe
Jun 25th, 1999, 08:27 PM
Hi,

I want to add a new record to my database on the form load event - that is, as soon as my form loads the text box will be blank, which will enable the user to input info that will then be added to the database.

When my code is this, however:

Private Sub Form_Load()
Data1.Recordset.AddNew
End Sub

I keep getting a run-time error '91'; object variable or With block not set

I'm sure there's some way around this, but I don't know how. Please help!

Thanks

VIKRAMAN.K
Jun 27th, 1999, 03:15 AM
First,you didn't mention in which Data Control or Data Object you have tried . I think you have tried with DAO. Any way, you have tried without declaring the database name and recordset name. The following statements will give the results what you have expected.

Dim d As Database
Dim r As Recordset

Private Sub Form_Load()
Set d = OpenDatabase("C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb")
Set r = d.OpenRecordset("Customers", dbOpenDynaset)
'set blank to all controls
r.addnew
End Sub


------------------

christophe
Jun 27th, 1999, 05:44 PM
VIKRAMAN.K

First of all, thanks for your help. Secondly, I tried what you said but it didn't work the way I wanted - when the form loads, it still starts with the first record in the database in my textbox.

How can I have it so that when the form loads the text box is blank - so someone can add a new record without having to click on an addnew button?

Here's the code I used. My database name is 'dictionary.mdb', and my table is called 'language'.

Thanks again!

Dim d As Database
Dim r As Recordset

Private Sub Form_Load()
Set d = OpenDatabase("D:\lingua-master\dictionary.mdb")
Set r = d.OpenRecordset("language", dbOpenDynaset)
'set blank to all controls
r.AddNew
End Sub

vikram.k
Jun 27th, 1999, 10:11 PM
Hi Christophe,

You have to give assignment statements instead of {'set blank to controls}. Anyway, there are two types of assingment in VB. If you have used control array of text controls, then give the following statements before or after r.addnew statement.

For i = 0 To Text1.Count - 1
Text1.Item(i).Text = ""
Next

otherwise, You have to assign blank value to all text box controls like text1.text = "",text2.text = "",... Instead of that you can give the following statements

Dim x As Control
For Each x In Controls
If TypeOf x Is TextBox Then
x.Text = ""
End If
Next

Bye!

christophe
Jun 28th, 1999, 01:29 AM
Hi VIKRAMAN.K,

Thanks again. But what am I doing wrong? (still no joy).

I have my code like this:

Dim d As Database
Dim r As Recordset

Private Sub Form_Load()
Setd=OpenDatabase("D:\lingua-master \dictionary. mdb")
Set r = d.OpenRecordset("language", dbOpenDynaset)
Text1.Text = ""
Text2.Text = ""
r.AddNew
End Sub

Note that I inserted Text1.Text = "" and Text2.Text = "", as you said. My form still loads with the first record in the text boxes. I have the recordsettype property of my data control set to Dynaset, if that's any help. Any ideas?

Yours sincerely,

Christophe

pmeredith
Aug 24th, 1999, 04:31 PM
I have learned that ther Form Load event is not the place to be opening databases and recordsets. The reason is that the order in which controls and such are initialized may screw things up. That is why you get the dreaded "With Object" error message. Try placing the code in the Form Activate event and see what happens.