从变量输入sql字符串中的日期

时间:2017-03-27 06:37:12

标签: vba access-vba

嘿伙计们我有这个代码....但它不会读我的日期我试着把“#”&变量和“#”

Private Sub Comando77_Click()
Dim nvoto As Single
Dim giorno As Date
Dim strsql As String
Dim codice As Integer
Dim vmateria As String

codice = Val(InputBox("inserire la matricola"))
giorno = InputBox("inserire la data")
nvoto = Val(InputBox("inserire il nuovo voto"))

If nvoto <> 0 Then
strsql = "Update Verifiche Set Voto=" & nvoto & " where Studente=" & codice & " and Data=" & VBA.Format(giorno, " dd/mm/yyyy")
DoCmd.RunSQL (strsql)
Me.Refresh
End If
End Sub

我的帐户被阻止发布

我有一些建议希望这个wrks为你。

ps:批准编辑;(

试试这个

Public t1 as Date
Public t2 as Date

t1 = #10/07/2012 01:13:17# '7th of October 2012
t2 = #10/10/2012 11:30:25# '10th of October 2012

sql = "select plate_no from INFO where date_time between #" + Format(t1, "YYYY-MM-DD HH:MM:SS") + "# and #" + Format(t2, "YYYY-MM-DD HH:MM:SS") + "#"

1 个答案:

答案 0 :(得分:0)

首先,InputBox始终返回文本,因此您必须将输入转换为日期值。然后将其格式化为适当的字符串表达式以与SQL连接。另外,正确格式化单值:

Private Sub Comando77_Click()

    Dim nvoto As Single
    Dim giorno As Date
    Dim strsql As String
    Dim codice As Integer
    Dim vmateria As String

    codice = Val(InputBox("inserire la matricola"))
    giorno = DateValue(InputBox("inserire la data"))
    nvoto = CSng(InputBox("inserire il nuovo voto"))

    If nvoto <> 0 Then
        strsql = "Update Verifiche Set Voto=" & Str(nvoto) & " where Studente=" & codice & " and Data=#" & VBA.Format(giorno, "yyyy\/mm\/dd") & "#"
        DoCmd.RunSQL strsql
        Me.Refresh
    End If

End Sub