Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    RecordSet example

    Would someone be able to give me a RecordSet example, please?

    I have an array consisting of 8 strings which I would like to write to a record using RecordSet and I don't have any examples in my very limited documentation.

    Thanks

    James

  2. #2
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RecordSet example

    I took this example from the Access 97 help:

    Sub EditX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim strOldFirst As String
    Dim strOldLast As String
    Dim strFirstName As String
    Dim strLastName As String

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set rstEmployees = _
    dbsNorthwind.OpenRecordset("Employees", _
    dbOpenDynaset)

    ' Store original data.
    strOldFirst = rstEmployees!FirstName
    strOldLast = rstEmployees!LastName

    ' Get new data for record.

    strFirstName = Trim(InputBox( _
    "Enter first name:"))
    strLastName = Trim(InputBox( _
    "Enter last name:"))

    ' Proceed if the user entered something for both fields.
    If strFirstName <> "" and strLastName <> "" Then
    ' Update record with new data.
    EditName rstEmployees, strFirstName, strLastName

    With rstEmployees
    ' Show old and new data.
    Debug.Print "Old data: " & strOldFirst & _
    " " & strOldLast
    Debug.Print "New data: " & !FirstName & _

    " " & !LastName
    ' Restore original data because this is a
    ' demonstration.
    .Edit
    !FirstName = strOldFirst
    !LastName = strOldLast
    .Update
    End With

    Else
    Debug.Print _
    "You must input a string for first and last name!"
    End If

    rstEmployees.Close
    dbsNorthwind.Close

    End Sub

    Sub EditName(rstTemp As Recordset, _
    strFirst As String, strLast As String)

    ' Make changes to record and set the bookmark to keep

    ' the same record current.
    With rstTemp
    .Edit
    !FirstName = strFirst
    !LastName = strLast
    .Update
    .Bookmark = .LastModified
    End With

    End Sub

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    The Netherlands
    Posts
    216
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RecordSet example

    And another example with the addnew method:

    Sub AddNewX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim strFirstName As String
    Dim strLastName As String

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set rstEmployees = _
    dbsNorthwind.OpenRecordset("Employees", dbOpenDynaset)

    ' Get data from the user.
    strFirstName = Trim(InputBox( _
    "Enter first name:"))
    strLastName = Trim(InputBox( _
    "Enter last name:"))

    ' Proceed only if the user actually entered something

    ' for both the first and last names.
    If strFirstName <> "" and strLastName <> "" Then

    ' Call the function that adds the record.
    AddName rstEmployees, strFirstName, strLastName

    ' Show the newly added data.
    With rstEmployees
    Debug.Print "New record: " & !FirstName & _
    " " & !LastName
    ' Delete new record because this is a demonstration.
    .Delete
    End With

    Else
    Debug.Print _
    "You must input a string for first and last name!"

    End If

    rstEmployees.Close
    dbsNorthwind.Close

    End Sub

    Function AddName(rstTemp As Recordset, _
    strFirst As String, strLast As String)

    ' Adds a new record to a Recordset using the data passed
    ' by the calling procedure. The new record is then made
    ' the current record.
    With rstTemp
    .AddNew
    !FirstName = strFirst
    !LastName = strLast
    .Update
    .Bookmark = .LastModified
    End With

    End Function

  4. #4
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Warrington (Greater Manchester area)/Cheshire, UK
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: RecordSet example

    Thanks for the examples, Bart.

    It should help quite a lot!

    James

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •