CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Posts
    41

    HELP !!! Updating Records in MSAccess fromVB

    Hi Guys,

    I am trying to update a field in a MS Access DB with the input from a ListView Control. But the problem is field in the MS Access DB has a releationship to a similar field in a different table (in the same DB) so its not letting me update this record. Any thoughts on getting around the problem??
    Thanks for your help ...
    Some of the code looks like as follows:

    Viji


    Public Sub AddRightsToDB()
    Dim SQL2 As String


    If Not cnn.State = adStateOpen Then
    OpenConnection
    End If

    If cnn.State = adStateOpen Then

    SQL2 = "update USER_RIGHTS set Access_Level = ('" & AccessLvl & "')where User_Name='" & Uname_Rights & "'"

    rstNewRights.Open SQL2, cnn, adOpenForwardOnly, adLockOptimistic

    CloseConnection


    End If
    End Sub




  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: HELP !!! Updating Records in MSAccess fromVB

    You're trying to open a recordset which contains a command to execute. You must do this in 2 steps: First execute the command, then reselect the record:

    public Sub AddRightsToDB()
    Dim SQL1 as string 'will hold statement to execute
    Dim SQL2 as string 'will hold statement to select

    If Not cnn.State = adStateOpen then
    OpenConnection
    End If

    If cnn.State = adStateOpen then

    SQL1 = "update USER_RIGHTS set Access_Level = ('" & AccessLvl & "')where User_Name='" & Uname_Rights & "'"
    SQL2 = "select * FROM USER_RIGHTS WHERE User_Name='" & Uname_Rights & "'"

    cnn.execute SQL1 ' do update
    rstNewRights.Open SQL2, cnn, adOpenForwardOnly, adLockOptimistic

    CloseConnection

    End If

    End Sub





    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    May 1999
    Posts
    41

    Thanks but there's a slight ProblemmVB

    Hi Tom,

    Thanks a lot...I tried but it still comes up with the following error message!!!

    "You can't add or change a Record because a releted record is required in table USER_GROUPS"

    This is because the field Access_Level cntains in both tables (USER_RIGHTS and USER_GROUPS) and are related ( defined using Relationship command in Access)
    Any thoughts are greatly appreciated.

    Viji



Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured