Access 2010 VBA ADODB Recordset仅返回一条记录

时间:2016-03-16 09:33:10

标签: sql-server vba adodb

我正在尝试和尝试几个小时,但找不到解决方案......以下是代码:

Private Sub Form_Load()
    Dim Cn As New ADODB.Connection
    Dim Rs As New ADODB.Recordset

    Cn.Provider = "SQLOLEDB.1"
    Cn.CursorLocation = adUseClient
    Cn.ConnectionString = "Password=*************;" & _
    "Persist Security Info=True;" & _
    "User ID=sa;" & _
    "Initial Catalog=pA-StdVK-KalkDB_V1;" & _
    "Data Source=WINSER27"
    Cn.Open

    Rs.Open "SELECT * FROM [dbo].[VIEW_Superliste_000]", Cn

    ' Me ist im Formular Name und Rs das Column im SQL Server
    Me.TeileGruppe.Value = Rs.Fields("Teilegruppe")
    Me.PlanTGrpSpanne_Stfl1.Value = Rs.Fields("Plan-TGrp-Spanne_Stfl1")
    Me.PlanTGrpSpanne_Stfl2.Value = Rs.Fields("Plan-TGrp-Spanne_Stfl2")
    Me.PlanTGrpSpanne_Stfl3.Value = Rs.Fields("Plan-TGrp-Spanne_Stfl3")
    ...
    Me.Suchbegriff.Value = Rs.Fields("Suchbegriff")
    Me.Selektion.Value = Rs.Fields("Selektion")
    Me.Katalogartikel.Value = Rs.Fields("Katalogartikel")
    Me.Sparte.Value = Rs.Fields("Sparte")

    Dim i As Long
    i = 0

    Do Until Rs.EOF
        i = i + 1
        Rs.MoveNext
    Loop

    Rs.Close


ExitSub:
    Set Rs = Nothing
    Exit Sub
ErrorHandler:
    Resume ExitSub
End Sub

使用...我在stackoverflow上缩短代码,这不是那么久!

为什么我只有一个记录???

1 个答案:

答案 0 :(得分:0)

尝试将代码更改为以下内容:

Rs.MoveFirst
Do Until Rs.EOF

Me.TeileGruppe.Value = Rs.Fields("Teilegruppe")
Me.PlanTGrpSpanne_Stfl1.Value = Rs.Fields("Plan-TGrp-Spanne_Stfl1")
Me.PlanTGrpSpanne_Stfl2.Value = Rs.Fields("Plan-TGrp-Spanne_Stfl2")
Me.PlanTGrpSpanne_Stfl3.Value = Rs.Fields("Plan-TGrp-Spanne_Stfl3")
...
Me.Suchbegriff.Value = Rs.Fields("Suchbegriff")
Me.Selektion.Value = Rs.Fields("Selektion")
Me.Katalogartikel.Value = Rs.Fields("Katalogartikel")
Me.Sparte.Value = Rs.Fields("Sparte")

Rs.MoveNext
Loop

在您的代码中,您将第一个记录集加载到表单中,然后循环遍历所有记录集,使用此代码,您遍历记录并将每个记录集加载到表单中。 但每次更换文本框的值时,您很可能只会看到最后一条记录。