Private Function GetSql(ByVal OpA As String, listctrl As ListBox) As String
'功能:返回SQL字符串
'参数:OpA -- Me.OpenArgs (写为:me.name & "子窗体控件名称")
' listctrl--存放所选字段的ListBox控件
Dim frm As Form
Dim A
Dim ssql As String, tb As String, wh As String
Dim i As Long
If listctrl.ListCount > 0 Then
A = Split(OpA, ";")
Set frm = Forms(A(0)).Controls(A(1)).Form
tb = Replace(frm.RecordSource, ";", "") 'From子句部分
wh = "True "
If Nz(frm.Filter, "") <> "" Then
wh = wh & " and " & frm.Filter 'Where子句部分
End If
ssql = "select "
For i = 0 To listctrl.ListCount - 1
ssql = ssql & listctrl.Column(0, i) & "," '拼接Select子句部分
Next
ssql = Left(ssql, Len(ssql) - 1) 'select子句部分
ssql = ssql & " from (" & tb & ") where " & wh
Else
ssql = ""
End If
GetSql = ssql
End Function