我多年没有做任何vba,完全忘记了怎么做 - 这就像重新学习vba一样!
我的表格中有一个"打开"事件来填充ado记录集中的数据。
我要做的是在顶部提供一些文本框,以允许用户输入一些条件并更改他们正在查看的记录集。
这是我的"开放"代码;
var tempArr = []
var result = {};
for(index in labels){
var elm = labels[index];
if(tempArr.indexOf(elm) > -1){
result[elm]= parseInt(barData[index]) + parseInt(result[elm]);
}
else{
tempArr.push(elm);
result[elm] = barData[index];
}
}
console.log(result);
bindFormData sub是;
Private Sub Form_Open(Cancel As Integer)
qString = "SELECT tblPerson.personID, [personLName]+', '+[personFName] AS fullName, tblFamily.familyAFCID, tblAddress.addressLine1, " & _
" tblFamily.personAFCID, refSource.sourceDescription AS personSource " & _
" FROM refSource RIGHT JOIN (((tblPerson LEFT JOIN tblFamily ON tblPerson.personID = tblFamily.personID) " & _
" LEFT JOIN lnkAddressPerson ON tblPerson.personID = lnkAddressPerson.personID) " & _
" LEFT JOIN tblAddress ON lnkAddressPerson.addressID = tblAddress.addressID) ON refSource.sourceID = tblPerson.personSource " & _
" WHERE lnkAddressPerson.addresslinkend is null "
qStringAppend = " order by personLName, personFName;"
bindFormData qString, qStringAppend, "frmPerson"
End Sub
这是我在命令按钮上尝试做的事情;
Public Sub bindFormData(ByVal qString As String, ByVal qStringAppend As String, ByVal formName As String)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CurrentProject.AccessConnection
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Source = qString + qStringAppend
.LockType = adLockBatchOptimistic
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.Open
End With
Set Forms(formName).Recordset = rs
Set rs = Nothing
Set cn = Nothing
End Sub
我的表单在open事件上正确填充,但在命令按钮单击时返回零记录。我在命令按钮上单击debug.print sql并验证它是否在查询中返回记录。
任何帮助表示感谢。
答案 0 :(得分:1)
Urrgh,多么恼人 - 在VBA中使用ADO记录集我需要使用%而不是*作为通配符......简单就是这样!
答案 1 :(得分:1)
一些想法
Changes:
Public Sub bindFormData(ByVal qString As String, ByVal qStringAppend As String, ByVal formName As String)
to
Public Sub bindFormData(ByVal qString As String, ByVal qStringAppend As String, ByVal form as Form)
Set Forms(formName).Recordset = rs
to
Set form.Recordset = rs
bindFormData qString, qStringAppend, "frmPerson"
to
bindFormData qString, qStringAppend, me.form
OR if you keep your existing code use this (in case you rename the form)
bindFormData qString, qStringAppend, me.form.Name
编写一个函数来构建并返回命令按钮和打开表单(最常见的!)之间通用的SQL文本。
使用我在答案here中概述的技术,使SQL更具可读性。
这是您要使用的SQL读取:
Public Sub aa(ByRef a As String, ByVal b As String)
' This is deliberately not a function, although it behaves a bit like one
a = a & vbCrLf & b
End Sub
' The function is called in code like this:
Dim a as string
a = ""
aa a, " SELECT tblPerson.personID "
aa a, " , [personLName]+', '+[personFName] AS fullName "
aa a, " , tblFamily.familyAFCID "
aa a, " , tblAddress.addressLine1 "
aa a, " , tblFamily.personAFCID "
aa a, " , refSource.sourceDescription AS personSource "
aa a, " FROM refSource "
aa a, " RIGHT JOIN (((tblPerson "
aa a, " LEFT JOIN tblFamily "
aa a, " ON tblPerson.personID = tblFamily.personID) "
aa a, " LEFT JOIN lnkAddressPerson "
aa a, " ON tblPerson.personID = lnkAddressPerson.personID) "
aa a, " LEFT JOIN tblAddress "
aa a, " ON lnkAddressPerson.addressID = tblAddress.addressID) "
aa a, " ON refSource.sourceID = tblPerson.personSource "
aa a, " WHERE lnkAddressPerson.addresslinkend IS NULL"