yolish
asked on
changing data in a database
I was wondering if it was possible to have a VB app loop through a sql database and change all values in a field from 1 to 2... If so how would I go about doing that.
Thanks-
Thanks-
DAO Method:
Dim DB as Database
Dim RS as Recordset
Set DB = opendatabase(app.path & "\mydb.mdb")
Set RS = DB.OpenRecordset("SELECT MyField FROM tblMyTable")
While NOT RS.EOF
RS.Edit
RS!MyField = 2
RS.Update
RS.MoveNext
Wend
Dim DB as Database
Dim RS as Recordset
Set DB = opendatabase(app.path & "\mydb.mdb")
Set RS = DB.OpenRecordset("SELECT MyField FROM tblMyTable")
While NOT RS.EOF
RS.Edit
RS!MyField = 2
RS.Update
RS.MoveNext
Wend
ADO Method:
Dim cnn As New ADODB.Connection
Dim rst as New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OL EDB.4.0;Da ta Source=C:\MyPath\MyDatabas e.mdb;"
rst.Open "SELECT MyField FROM tblMyTable", cnn
While Not rst.EOF
rst.Edit
rst.Fields("MyField").Valu e = "New Name"
rst.Update
wend
rst.Close
cnn.Close
Dim cnn As New ADODB.Connection
Dim rst as New ADODB.Recordset
cnn.Open "Provider=Microsoft.Jet.OL
rst.Open "SELECT MyField FROM tblMyTable", cnn
While Not rst.EOF
rst.Edit
rst.Fields("MyField").Valu
rst.Update
wend
rst.Close
cnn.Close
ASKER
The code looks good here is what I did...
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "dsn=VendorRequest;uid=sa; pwd=majest ic;"
rst.Open "SELECT Approval_Status FROM WEB_VENDOR WHERE Approval_Status=5", cnn
While Not rst.EOF
rst.Edit
rst.Fields("Approval_Statu s").Value = 73
rst.Update
Wend
rst.Close
cnn.Close
End Sub
But when I goto run it I get user defined type not defined for the 2 dim's...
Thanks-
Private Sub Form_Load()
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
cnn.Open "dsn=VendorRequest;uid=sa;
rst.Open "SELECT Approval_Status FROM WEB_VENDOR WHERE Approval_Status=5", cnn
While Not rst.EOF
rst.Edit
rst.Fields("Approval_Statu
rst.Update
Wend
rst.Close
cnn.Close
End Sub
But when I goto run it I get user defined type not defined for the 2 dim's...
Thanks-
Or you could try:
Dim DB as Database
Set DB = opendatabase(app.path & "\mydb.mdb")
Call DB.Execute("UPDATE tblMyTable SET MyField = 2 WHERE MyField = 1", dbFailOnError)
Dim DB as Database
Set DB = opendatabase(app.path & "\mydb.mdb")
Call DB.Execute("UPDATE tblMyTable SET MyField = 2 WHERE MyField = 1", dbFailOnError)
Ah LearnedOne, I didn't even think of the query method. Much better.
Yolish - You need to proper references in your app:
To add these references in Visual Basic:
From the Project menu select References…
From the list, select "Microsoft ActiveX Data Objects 2.1 Library."
From the list, select "Microsoft ADO Ext. 2.1 for DDL and Security."
From the list, select "Microsoft Jet and Replication Objects 2.1 Library."
Click OK
Yolish - You need to proper references in your app:
To add these references in Visual Basic:
From the Project menu select References…
From the list, select "Microsoft ActiveX Data Objects 2.1 Library."
From the list, select "Microsoft ADO Ext. 2.1 for DDL and Security."
From the list, select "Microsoft Jet and Replication Objects 2.1 Library."
Click OK
ASKER
that did it.... but now it runs into an error with the edit saying that Method or data member not found...
Thanks again
Thanks again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I guess I could have done this all in SQL server....So thanks...
Dim DB as Database
Dim RS as Recordset
Set DB = opendatabase(app.path & "\mydb.mdb")
Set RS = DB.OpenRecordset("SELECT MyField FROM tblMyTable")
While NOT RS.EOF
RS.Edit
RS!MyField = 2
RS.Update
RS.MoveNext
Wend