Access VBA how can I .Seek a value in a recordset, error code '3251' operation is not supported for this type of object

放肆的年华 提交于 2019-12-11 17:56:15

问题


I'm having difficulty writing VBA to accomplish a couple goals: 1. loop through address table ("SunstarAccountsInWebir_SarahTest") to separate out valid from invalid addresses. if invalid - export to different table. 2. If valid, match the address values to a 2nd table. If it matches, then insert the address values into the 2nd table.
3. If it doesn't match, then export to another different table

The issue I'm getting is the exporting works, but I'm not seeing any action on condition 2 "valid address where it's ID is in the 2nd table". I'm trying to use seek method to see if the ID from the first table is in the 2nd table - and have it update as many times as it occurs (ID's are not unique in both tables). And I can't use two nested loops because the tables are too large. Below is the code as of now. I'm returning an error where it says: StrSQL1.Index = "blablavalue"

Public Sub EditFinalOutput2()

'set variables
Dim i As Long
Dim qs As DAO.Recordset
Dim ss As DAO.Recordset
Dim StrSQL1 As DAO.Recordset
Dim IRSfileFormatKey As String
Dim external_nmad_id As String
Dim nmad_address_1 As String
Dim nmad_address_2 As String
Dim nmad_address_3 As String
Dim mytestwrite As String

'open reference set
Set db = CurrentDb
Set qs = db.OpenRecordset("SunstarAccountsInWebir_SarahTest")

With qs.Fields
intCount = qs.RecordCount - 1
For i = 0 To intCount

If (IsNull(!nmad_address_1) Or (!nmad_address_1 = !nmad_city) Or (!nmad_address_1 = !Webir_Country) And IsNull(!nmad_address_2) Or (!nmad_address_2 = !nmad_city) Or (!nmad_address_2 = !Webir_Country) And IsNull(!nmad_address_3) Or (!nmad_address_3 = !nmad_city) Or (!nmad_address_3 = !Webir_Country)) Then
DoCmd.RunSQL "INSERT INTO Addresses_ToBeReviewed SELECT SunstarAccountsInWebir_SarahTest.* FROM SunstarAccountsInWebir_SarahTest WHERE (((SunstarAccountsInWebir_SarahTest.external_nmad_id)='" & qs!external_nmad_id & "'));"

Else:
    Set StrSQL1 = db.OpenRecordset("SELECT RIGHT(IRSfileFormatKey, 10) As blablavalue FROM 1042s_FinalOutput_7;", dbOpenDynaset)
    Set ss = db.OpenRecordset("1042s_FinalOutput_7")
    StrSQL1.Index = "blablavalue"
    StrSQL1.Seek "=", !external_nmad_id

        If ss.NoMatch Then
        DoCmd.RunSQL "INSERT INTO Addresses_NotUsed SELECT SunstarAccountsInWebir_SarahTest.* FROM SunstarAccountsInWebir_SarahTest WHERE (((SunstarAccountsInWebir_SarahTest.external_nmad_id)='" & qs!external_nmad_id & "'));"

        Else:   Set ss = db.OpenRecordset("1042s_FinalOutput_7")
                ss.Edit
                ss.Fields("box13c_Address") = qs.Fields("nmad_address_1") & qs.Fields("nmad_address_2") & qs.Fields("nmad_address_3")
                ss.Update

        End If
End If

qs.MoveNext
Next i

End With

'close reference set
qs.Close
Set qs = Nothing
ss.Close
Set ss = Nothing

End Sub

回答1:


Thanks to @Hansup for providing guidance, the final "working" code looks like this:

Public Sub EditFinalOutput2()

'set variables
Dim i As Long
Dim qs As DAO.Recordset
Dim ss As DAO.Recordset
Dim StrSQL1 As DAO.Recordset
Dim IRSfileFormatKey As String
Dim external_nmad_id As String
Dim nmad_address_1 As String
Dim nmad_address_2 As String
Dim nmad_address_3 As String
Dim mytestwrite As String
Dim PrimaryKey As String
Dim box13c_Address As String

'open reference set
Set db = CurrentDb
Set qs = db.OpenRecordset("SunstarAccountsInWebir_SarahTest")

'turn popup messages off
DoCmd.SetWarnings False

With qs.Fields
intCount = qs.RecordCount - 1
For i = 0 To intCount

If (IsNull(!nmad_address_1) Or (!nmad_address_1 = !nmad_city) Or (!nmad_address_1 = !Webir_Country) And IsNull(!nmad_address_2) Or (!nmad_address_2 = !nmad_city) Or (!nmad_address_2 = !Webir_Country) And IsNull(!nmad_address_3) Or (!nmad_address_3 = !nmad_city) Or (!nmad_address_3 = !Webir_Country)) Then
DoCmd.RunSQL "INSERT INTO Addresses_ToBeReviewed SELECT SunstarAccountsInWebir_SarahTest.* FROM SunstarAccountsInWebir_SarahTest WHERE (((SunstarAccountsInWebir_SarahTest.external_nmad_id)='" & qs!external_nmad_id & "'));"

Else:
    Set StrSQL1 = db.OpenRecordset("SELECT RIGHT([1042s_FinalOutput_7].IRSfileFormatKey,  10) As PrimaryKey, box13c_Address FROM 1042s_FinalOutput_7;", dbOpenDynaset)
    StrSQL1.FindFirst ([PrimaryKey] = qs.Fields("external_nmad_id"))

        If StrSQL1.NoMatch Then
        DoCmd.RunSQL "INSERT INTO Addresses_NotUsed SELECT SunstarAccountsInWebir_SarahTest.* FROM SunstarAccountsInWebir_SarahTest WHERE (((SunstarAccountsInWebir_SarahTest.external_nmad_id)='" & qs!external_nmad_id & "'));"

        Else:
                StrSQL1.Edit
                StrSQL1.Fields("box13c_Address") = qs.Fields("nmad_address_1") & qs.Fields("nmad_address_2") & qs.Fields("nmad_address_3")
                StrSQL1.Update

        End If

End If

qs.MoveNext
Next i

End With

'turn popup messages back on
DoCmd.SetWarnings True

'close reference set
qs.Close
Set qs = Nothing
ss.Close
Set ss = Nothing

End Sub


来源:https://stackoverflow.com/questions/52303535/access-vba-how-can-i-seek-a-value-in-a-recordset-error-code-3251-operation-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!