Access Run time error - '-2147352567 (80020009)': subform

雨燕双飞 提交于 2021-02-10 07:01:22

问题


I've created a form where I can use a subform to load data from a query into textboxes so I can edit the data one at a time and update the database. This usually works fine except I have a run time error that randomly appears now and then - '-2147352567 (80020009)': The value you entered isn't valid for this field.'

If I stop running it and try running it again it usually works fine until the same run-time error appears. It's very inconsistent.

    Private Sub btn_SelectAgency_Click()

If Not (Me.qryAgencyWithoutMileagesub.Form.Recordset.EOF And Me.qryAgencyWithoutMileagesub.Form.Recordset.BOF) Then

'get data to textbox control
    With Me.qryAgencyWithoutMileagesub.Form.Recordset
        Me.txt_AgencyID = .Fields("Agency ID")    
        Me.txt_AgencyName = .Fields("Agency Name")
        Me.txt_Address1 = .Fields("Address 1")
        Me.txt_Address2 = .Fields("Address 2")
        Me.txt_City = .Fields("City")
        Me.txt_Postcode = .Fields("Postcode")
        Me.txt_AgencyMileage = .Fields("Mileage")
        Me.txt_AgencyID.Tag = .Fields("Agency ID")

    End With
End If

Me.txt_AgencyMileage = ""

End Sub


Private Sub btn_Update_Click()

If Me.txt_AgencyMileage = "" Then
    MsgBox "No mileage added, add now"
    Cancel = True
Else
    CurrentDb.Execute "UPDATE EstateAgent_tbl SET EstateAgent_AgentMileage = '" & Me.txt_AgencyMileage & "' where EstateAgent_AgentID=" & Me.txt_AgencyID.Tag
    Me.txt_AgencyID = ""
    Me.txt_AgencyName = ""
    Me.txt_Address1 = ""
    Me.txt_Address2 = ""
    Me.txt_City = ""
    Me.txt_Postcode = ""
    Me.txt_AgencyMileage = ""
End If

qryAgencyWithoutMileagesub.Form.Requery

    If Me.qryAgencyWithoutMileagesub.Form.Recordset.RecordCount = 0 Then
        MsgBox "No agencies without mileage"
        DoCmd.Close
    End If

End Sub

The error is on this line

Me.txt_AgencyID = .Fields("Agency ID")          

I would appreciate any help with this, thank you :)


回答1:


Dim rs As DAO.Recordset    
Set rs = Me.qryAgencyWithoutMileagesub.Form.RecordsetClone
    'get data to textbox control
With rs
    If .RecordCount > 0 Then
        Me.txt_AgencyID = .Fields("Agency ID")    
        Me.txt_AgencyName = .Fields("Agency Name")
        Me.txt_Address1 = .Fields("Address 1")
        Me.txt_Address2 = .Fields("Address 2")
        Me.txt_City = .Fields("City")
        Me.txt_Postcode = .Fields("Postcode")
        Me.txt_AgencyMileage = .Fields("Mileage")
        Me.txt_AgencyID.Tag = .Fields("Agency ID")
    End If
End With

Set rs = Nothing


DoCmd.RefreshRecord 

DoCmd.RunCommand acCmdUndo

'You can it try. That worked for me!




回答2:


Try to exclude Null values:

If Not IsNull(.Fields("Agency ID").Value) Then        
    Me.txt_AgencyID.Value = .Fields("Agency ID").Value
End If

You may also try using the RecordsetClone:

Dim rs As DAO.Recordset

Set rs = Me.qryAgencyWithoutMileagesub.Form.RecordsetClone

        'get data to textbox control
    With rs
        If .RecordCount > 0 Then
            Me.txt_AgencyID = .Fields("Agency ID")    
            Me.txt_AgencyName = .Fields("Agency Name")
            Me.txt_Address1 = .Fields("Address 1")
            Me.txt_Address2 = .Fields("Address 2")
            Me.txt_City = .Fields("City")
            Me.txt_Postcode = .Fields("Postcode")
            Me.txt_AgencyMileage = .Fields("Mileage")
            Me.txt_AgencyID.Tag = .Fields("Agency ID")
        End If
    End With

Set rs = Nothing


来源:https://stackoverflow.com/questions/41718872/access-run-time-error-2147352567-80020009-subform

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