• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

Visual Basic DAO code

Hi,
As I have not used VB for a very long time, thus my request here.  Can someone please write the VB code to do the following which I intend to put it in a Form :
1. Retrieve a table
2. Go to the beginning of the record, and then start looping one record at a time until EOF is reached.
3. retrieve the contents of the field "ID" and pass it to a variable.
4. [ here I will write the code what to do with the variable ]
5. Then assign the value of that variable to another field called "CheckDigit". this field is within the same Table.
6. Loop to the next record -- until the end of the records is reached.

Thanks.
Jothy

0
jegajothy
Asked:
jegajothy
  • 3
  • 3
  • 2
1 Solution
 
vb_jonasCommented:
Are you sure you would like to use DAO? ADO.NET is much more up to date.

If DAO:
http://www.dotnet247.com/247reference/msgs/18/90914.aspx

If ADO.NET:
0
 
jegajothyretiredAuthor Commented:
I am lost after the line Move First
Could you please write me the code to assign a variable to a field called ID in the Table.
Also why the
 For x = 0 to 5 -- hope this is the right syntax as I am typing from memory.
thanks.
Jothy
0
 
GrahamSkanRetiredCommented:
Option Explicit

Private Sub Command1_Click()
Dim db As Database
Dim rs As Recordset
    Set db = OpenDatabase("C:\Documents and Settings\user name\My Documents\Generaltest.mdb")
        Set rs = db.OpenRecordset("MyTable")
            rs.MoveFirst
            Do Until rs.EOF
                Myvariable = rs.Fields("ID")
                'your code
                rs.MoveNext
            Loop
        rs.Close
    db.Close
End Sub
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
jegajothyretiredAuthor Commented:
Graham,
The syntax : Myvariable = rs.Fields("ID")
If I understand it correctly, it assigns from right to left, is this correct. i.e. the contents of the field ID is transferred to Myvariable.
I want a rs.Fields("ID") = myvariable.  That is the contents of myvariable to go into the field "ID".
Is this the correct syntax.
Thanks.
Jothy
0
 
GrahamSkanRetiredCommented:
You are right. Silly mistake. It reveals that I didn't actually test the code.

0
 
vb_jonasCommented:
You will need to do this to edit

rs.Edit
rs.Fields("ID")=myvariable
rs.Update

0
 
GrahamSkanRetiredCommented:
Careful reading of your requirement, third thoughts and a prompt from vb_jonas:

Private Sub Command1_Click()
Dim db As Database
Dim rs As Recordset
    Set db = OpenDatabase("C:\Documents and Settings\user name\My Documents\Generaltest.mdb")
        Set rs = db.OpenRecordset("MyTable")
            rs.MoveFirst
            Do Until rs.EOF
                Myvariable = rs.Fields("ID")
               
                'your code
                rs.Edit
                rs.Fields("CheckDigit") = MyVariable
                rs.Update
                rs.MoveNex
            Loop
        rs.Close
    db.Close
End Sub
0
 
jegajothyretiredAuthor Commented:
Bravo for an assignment well done.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now