MS Access - VBA 当 TxtBox 不为空时执行

MS Access - VBA Do while TxtBox is Not Empty

Public Sub Ohno()
Dim stsql As String, results As String
Dim rs As Object, Db As Object, con As Object
Dim num As Integer
Dim start As Object

Set Db = CurrentDb()
Set con = Application.CurrentProject.Connection
Set rs = CreateObject("ADODB.Recordset")

num = 1
For num = 1 To 2
  Do While IsEmpty(Forms("setup").Controls("TxtBoxEntry" & num)) = False
    Set start = Forms("setup").Controls("TxtBoxEntry" & num)
    stsql = "SELECT [Crosswalk].[Oracle GL Acct] FROM Crosswalk WHERE [Crosswalk].[Legacy GL Acct]= '" & start & "' "
    rs.Open stsql, con
    results = rs(0).Value
    Forms("setup").Controls("TxtBoxRslt" & num).Value = results
  Loop
Next

Set con = Nothing
Set rs = Nothing

我不断收到:Operation isn't allow while Object is Open - click me 该代码确实适用于第一个 txtbox 并停止给我上述错误。我是不是在为这个失败做好准备?

您需要在使用后关闭记录集。尝试添加 rs.Close:

Set start = Forms("setup").Controls("TxtBoxEntry" & num)
stsql = "SELECT [Crosswalk].[Oracle GL Acct] 
         FROM Crosswalk WHERE [Crosswalk].[Legacy GL Acct]= '" & start & "' "
rs.Open stsql, con
results = rs(0).Value
Forms("setup").Controls("TxtBoxRslt" & num).Value = results
rs.Close   -- Add this here