双击列表框Access中的sql结果

时间:2013-08-12 13:19:21

标签: vba ms-access listbox

我一直在寻找stackoverflow的答案,但我还没找到。所以我决定问自己的问题。

我在VBA中编码搜索引擎,以便在我的数据库中找到合同。

这是我迄今为止所做的事情

Private Sub cmd_recherche_Click()

Dim strTable As String, strField As String, strCriteria As String, strSql As String
Dim Criter As Variant

If Me.listbox_search = "Num_contract" Then

Debug.Print Me.listbox_search

strTable = "AFN_LOT0"     
strField = "NUMERO"         


strCriteria = strTable & "." & strField & " Like """ & Me.txt_critere & """"


strSql = "SELECT DISTINCTROW " & strTable & "." & strField & "," & strTable & ".FORMULE," & strTable & ".DATE_EFFET," & strTable & ".DATE_ECHEANCE," & strTable & ".ETAT," & strTable & ".CODE_RESILIATION," & strTable & ".DATE_RESIL," & strTable & ".DATE_OPERATION"
strSql = strSql & " FROM " & strTable
strSql = strSql & " WHERE " & strCriteria & ";"


Me.lst_resultat.RowSource = strSql  
Me.lst_resultat.Requery

End if 
End sub

现在我希望能够双击列表框中的结果(lst_resultat)并打开另一个表单(F_InfosContract),其中包含合同的所有信息(在单独的表中发送,所有表格都链接在一起)在列表框中选择的字段ID_SOR。

我已尝试过此功能,但它只打开列表框中没有任何内容的表单。

Private Sub lst_resultat_DblClick(Cancel As Integer)
Dim stLinkCriteria As String
Dim Selection As String

Selection = lst_resultat.Value
stLinkCriteria = Selection

DoCmd.OpenForm "F_InfosContract", , , stLinkCriteria
End Sub

如果有人可以帮助我,那就太好了

抱歉我的英文

Mdgy

1 个答案:

答案 0 :(得分:0)

DoCmd.OpenForm "F_InfosContract", , , stLinkCriteria

您只是将所选值作为stLinkCriteria提供,这需要正确形成Where Condition。举个例子:

DoCmd.OpenForm "frmStaff", , , "StaffID=" & Me.List1.Value

StaffID是另一个表中字段的名称,在您的情况下是ID_SOR。

如果id是文本字段,那么值需要用撇号包围:

DoCmd.OpenForm "frmStaff", , , "StaffID='" & Me.List1.Value & "'"