根据文本框值调用记录集

时间:2019-04-15 15:34:14

标签: sql excel vba

我需要调出一组存储在MS Access数据库中的记录。到目前为止,我已经设法显示所有记录,但是我需要根据作品集编号显示特定的记录,这些记录存储在用户窗体文本框中。

我尝试多次更改WHERE Folio = me.tbFolio.Value,但是我做对了,只是不断显示数据库上的所有记录集。

Private Sub Srch_Click()
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long, sSQL As String, Fl As Long
    Let Fl = tbFolio.Value - 1
    Set cn = New ADODB.Connection
    cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
        "Data Source=S:\Common\Quality\RASTREABILIDAD\MAIN PROJECT\PROYECTO KOREANO MX.accdb;"
    Set rs = New ADODB.Recordset
    rs.Open "Trazabilidad", cn, adOpenStatic, adLockReadOnly, adCmdTable
    sSQL = "SELECT [Folio], [N° de Orden, Fecha], [N° de Parte, Materiales], [N° de Parte Material], [N° de Lote/Fecha de Proucción], [Quién Capturo]" & _
        "FROM Trazabilidad" & _
        "WHERE Folio = me.tbfolio.value;"
    MsgBox rs.GetString, vbOKOnly, "Trazabilidad"
    cn.Close
    Set cn = Nothing
End Sub

1 个答案:

答案 0 :(得分:0)

您应该尝试从文本框中连接(使用&)值,以将其 查询中的值

  Private Sub Srch_Click()
    Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long, sSQL As String, Fl As Long
    Let Fl = tbFolio.Value - 1
        Set cn = New ADODB.Connection
        cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
            "Data Source=S:\Common\Quality\RASTREABILIDAD\MAIN PROJECT\PROYECTO KOREANO MX.accdb;"
        Set rs = New ADODB.Recordset
        rs.Open "Trazabilidad", cn, adOpenStatic, adLockReadOnly, adCmdTable
        sSQL = "SELECT [Folio], [N° de Orden, Fecha], [N° de Parte, Materiales], [N° de Parte Material], [N° de Lote/Fecha de Proucción], [Quién Capturo]" & _
            "FROM Trazabilidad" & _
            "WHERE Folio = " &  me.tbfolio.value
        MsgBox rs.GetString, vbOKOnly, "Trazabilidad"
        cn.Close
        Set cn = Nothing
    End Sub

如果是字符串 更改

"WHERE Folio = " &  me.tbfolio.value 

对于

"WHERE Folio = '" &  me.tbfolio.value & "'"