Procedure with 2 record sets one select, one update 
Author Message
 Procedure with 2 record sets one select, one update

Trying to do some data cleanup by looking up a field, doing using some
string functions to clean it up, then updating the record. I tried to do
this by opening one recordset with a select statement, then modifying the
variables, updating the record by opening a second record set. All my
variable values work great, and I am able to build the proper update
statement, by I get and "invalid use of property" when I try to use:
set RS1 = db.OpenRecordset(UpdateString)
where the UpdateString is the value of the update sql statement. Any help is
appreciated. My code is attached below.

Private Sub Garret()
Dim db As Database
Dim RS As Recordset
Dim RS1 As Recordset 'second recordset to do the record update

Dim Count As Integer
Dim SelectString As String  'SQL string to select all the records from the
table
Dim UpdateString As String  'SQL string to update the records with the new
job id
Dim NewJobId As String      'Right4 plus the right left RMJ, F, WRJ
Dim Right4 As String        '4 characters from right of jobID
Dim Left1 As String         'First character of job id to determine if it is
rmj, f, or wrj

SelectString = "SELECT * FROM garret Where [Job Number] is Not Null"

Set db = CurrentDb()
Set RS = db.OpenRecordset(SelectString)

Do While Not RS.EOF
    'Cut the rightmost 4 characters from JOBID
    Right4 = Right(RS![Job Number], 4)

    'Determine what the starting character of the JOBID is
    Left1 = Left(RS![Job Number], 1)

    'MsgBox ("left1 =" & Left1 & " Righ4 =" & Right4)

    'Add the appropriate begin to the right4 depend if it is an
outofwarranty RMJ in warranty wrJ or F
    If Left1 = "R" Then
        NewJobId = "RMJ" & Right4
        ElseIf Left1 = "W" Then
        NewJobId = "WRJ" & Right4
        ElseIf Left1 = "F" Then
        NewJobId = "F" & Right4
        Else
        NewJobId = RS![Job Number]
    End If
    'MsgBox ("NewJobID = " & NewJobId)

    'Create the UPDATE string to update the record with the NewJobID
    UpdateString = "Update Garret Set Garret![Job Number] = "
    UpdateString = UpdateString & "'" & NewJobId & "'" & " Where Garret![Job
Number] = " & "'" & RS![Job Number] & "';"
    'MsgBox (UpdateString)
    Set RS1 = db.OpenRecordset(UpdateString)
    RS1.Close
    RS.MoveNext
Loop

RS.Close

End Sub

aka G



Mon, 07 Jun 2004 07:28:29 GMT  
 Procedure with 2 record sets one select, one update
You don't need two separate recordsets, you can do the updates in the same
recordset -

delete all of these lines:

Quote:
> 'Create the UPDATE string to update the record with the NewJobID
>     UpdateString = "Update Garret Set Garret![Job Number] = "
>     UpdateString = UpdateString & "'" & NewJobId & "'" & " Where
Garret![Job
> Number] = " & "'" & RS![Job Number] & "';"
>     'MsgBox (UpdateString)
>     Set RS1 = db.OpenRecordset(UpdateString)
>     RS1.Close

Replace them with:

    rs.edit
    rs.fields("Job Number")= newjobid
    rs.update

Also FWIW, you could do all this in an update query -
--

Sandra Daigle, Microsoft Access MVP


Quote:
> Trying to do some data cleanup by looking up a field, doing using some
> string functions to clean it up, then updating the record. I tried to do
> this by opening one recordset with a select statement, then modifying the
> variables, updating the record by opening a second record set. All my
> variable values work great, and I am able to build the proper update
> statement, by I get and "invalid use of property" when I try to use:
> set RS1 = db.OpenRecordset(UpdateString)
> where the UpdateString is the value of the update sql statement. Any help
is
> appreciated. My code is attached below.

> Private Sub Garret()
> Dim db As Database
> Dim RS As Recordset
> Dim RS1 As Recordset 'second recordset to do the record update

> Dim Count As Integer
> Dim SelectString As String  'SQL string to select all the records from the
> table
> Dim UpdateString As String  'SQL string to update the records with the new
> job id
> Dim NewJobId As String      'Right4 plus the right left RMJ, F, WRJ
> Dim Right4 As String        '4 characters from right of jobID
> Dim Left1 As String         'First character of job id to determine if it
is
> rmj, f, or wrj

> SelectString = "SELECT * FROM garret Where [Job Number] is Not Null"

> Set db = CurrentDb()
> Set RS = db.OpenRecordset(SelectString)

> Do While Not RS.EOF
>     'Cut the rightmost 4 characters from JOBID
>     Right4 = Right(RS![Job Number], 4)

>     'Determine what the starting character of the JOBID is
>     Left1 = Left(RS![Job Number], 1)

>     'MsgBox ("left1 =" & Left1 & " Righ4 =" & Right4)

>     'Add the appropriate begin to the right4 depend if it is an
> outofwarranty RMJ in warranty wrJ or F
>     If Left1 = "R" Then
>         NewJobId = "RMJ" & Right4
>         ElseIf Left1 = "W" Then
>         NewJobId = "WRJ" & Right4
>         ElseIf Left1 = "F" Then
>         NewJobId = "F" & Right4
>         Else
>         NewJobId = RS![Job Number]
>     End If
>     'MsgBox ("NewJobID = " & NewJobId)

>     'Create the UPDATE string to update the record with the NewJobID
>     UpdateString = "Update Garret Set Garret![Job Number] = "
>     UpdateString = UpdateString & "'" & NewJobId & "'" & " Where
Garret![Job
> Number] = " & "'" & RS![Job Number] & "';"
>     'MsgBox (UpdateString)
>     Set RS1 = db.OpenRecordset(UpdateString)
>     RS1.Close
>     RS.MoveNext
> Loop

> RS.Close

> End Sub

> aka G



Mon, 07 Jun 2004 09:08:35 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Fast Way to update one field in one record

2. Finding Records and Updating Records in More than one Recordsets

3. changes to all records in one rec set based on position in another rec set

4. How to retrieve 2 select statements from one Stored Procedure

5. Updating the one-side of an one-to-many relationship

6. Auto create record for one-to-one relationship tables

7. Adding records with one-to-one relationship

8. How to display one record on one line in a listbox

9. One record on one page...

10. Print one record from one simple Acess dababase with Adodc

11. ASAP: How do u set the cmdOK_Click event to have more than one event procedure

12. Getting both result sets into one recordset from a single stored procedure

 

 
Powered by phpBB® Forum Software