Basic Visual Basic 程序错误 - 可能是变量/语法错误(标记为 Fat)

Posted

技术标签:

【中文标题】Basic Visual Basic 程序错误 - 可能是变量/语法错误(标记为 Fat)【英文标题】:Basic Visual Basic Program Error - Probably Variable/ Syntax Error (marked Fat) 【发布时间】:2021-08-16 16:09:57 【问题描述】:

这是编辑后的代码,带有 Gutav 的功能和 André 实现的代码。

它似乎仍然无法正常工作。我一无所知。

新代码:

Option Explicit
Private Sub Form_Load()
    
    Dim rs          As Recordset
    Dim rs2         As Recordset
    Dim Werte       As String
    Dim Datums_variable As Date
    Dim actual_date As Date
    Dim id          As Integer
    Dim strSql      As String
    
    actual_date = Date
    
    Set rs = CurrentDb.OpenRecordset("select * from query_offene_wartungspunkte ", dbOpenDynaset)
    
    Me.Test_Liste.RowSourceType = " Value List "
    
    Do Until rs.EOF
        
        Werte = rs!intervall
        
        Select Case Werte
            
            Case "jährlich"
                Datums_variable = actual_date - 365
            Case "halbjährlich"
                Datums_variable = actual_date - 180
            Case "monatlich"
                Datums_variable = actual_date - 31
            Case "wöchentlich"
                Datums_variable = actual_date - 7
            Case "täglich"
                Datums_variable = actual_date - 1
            Case "vierteljährlich"
                Datums_variable = actual_date - 90
                
        End Select
        
        id = rs!id
        
        strSql = "select * from query_offene_wartungspunkte " & _
                 "where [Date] > " & CSql(Datums_variable) & " And id = " & id
        Debug.Print strSql

        Set rs2 = CurrentDb.OpenRecordset(strSql, dbOpenDynaset)
        
        If Not rs2.EOF Then
            Werte = " Arbeitsplatz/Maschine: " & rs2!arbeitsplatz_maschine & " ArbeitsplatzNr: " & rs2!arbeitsplatz_nr
            Me.Test_Liste.AddItem (Werte)
            
        Else
            ' rs2 is empty
            
            
        End If
        
        rs.MoveNext
    Loop
End Sub


Public Function CSql( _
       ByVal Value As Variant) _
       As String
    
    #If Win32 Then
        ' Serves only to make the code compile unmodified in 32-bit VBA
        ' which misses the constant VBA.vbLongLong.
        Const vbLongLong    As Integer = 20
    #End If
    
    Const SqlNull           As String = " Null"
    
    Dim Sql                 As String
    
    Select Case VarType(Value)
        Case vbEmpty        '    0  Empty (uninitialized).
            Sql = SqlNull
        Case vbNull        '    1  Null (no valid data).
            Sql = SqlNull
        Case vbInteger        '    2  Integer.
            Sql = Str(Value)
        Case vbLong        '    3  Long integer.
            Sql = Str(Value)
        Case vbSingle        '    4  Single-precision floating-point number.
            Sql = Str(Value)
        Case vbDouble        '    5  Double-precision floating-point number.
            Sql = Str(Value)
        Case vbCurrency        '    6  Currency.
            Sql = Str(Value)
        Case vbDate        '    7  Date.
            Sql = Format(Value, " \#yyyy\/mm\/dd hh\:nn\:ss\#")
        Case vbString        '    8  String.
            Sql = Replace(Trim(Value), "'", "''")
            If Sql = "" Then
                Sql = SqlNull
            Else
                Sql = "        '" & Sql & "'"
            End If
        Case vbObject        '    9  Object.
            Sql = SqlNull
        Case vbError        '   10  Error.
            Sql = SqlNull
        Case vbBoolean        '   11  Boolean.
            Sql = Str(Abs(Value))
        Case vbVariant        '   12  Variant (used only with arrays of variants).
            Sql = SqlNull
        Case vbDataObject        '   13  A data access object.
            Sql = SqlNull
        Case vbDecimal        '   14  Decimal.
            Sql = Str(Value)
        Case vbByte        '   17  Byte.
            Sql = Str(Value)
        Case vbLongLong        '   20  LongLong integer (Relevant in 64-bit VBA only).
            Sql = Str(Value)
        Case vbUserDefinedType        '   36  Variants that contain user-defined types.
            Sql = SqlNull
        Case vbArray        ' 8192  Array. Ignored.
            Sql = SqlNull
        Case Else        '       Should not happen.
            Sql = SqlNull
    End Select
    
    CSql = Sql & " "
    
End Function

有人可以帮帮我吗?


像这样:

Set rs2 = CurrentDb.OpenRecordset("select * from query_offene_wartungspunkte where Date > " & Datums_variable & " AND id = " & id = " & id, dbOpenDynaset)

我试过了,但它也会抛出一个错误

【问题讨论】:

请看***.com/q/332365/11683。 应该是Date > " & Datums_variable & " AND id = " & id 像这样: Set rs2 = CurrentDb.OpenRecordset("select * from queerry_offene_wartungspunkte where Date > " & Datums_variable & " AND id = " & id = " & id, dbOpenDynaset) 我试过这个,但它也给出了一个错误。 日期参数值需要用单引号括起来,或者,对于 MS Access,井号 (#)。 【参考方案1】:

如果您想用 SQL 连接变量,请使用 Gustav 的CSql() function。 它处理日期、字符串和其他变量。

strSql = "select * from query_offene_wartungspunkte " & _
         "where [Date] > " & CSql(Datums_variable) & " AND id = " & id
Debug.Print strSql
Set rs2 = CurrentDb.OpenRecordset(strSql, dbOpenDynaset)

为什么是Debug.Print?见How to debug dynamic SQL in VBA

检查记录集是否有行的正确方法是:

If Not rs2.EOF Then
    ' use values
Else
    ' rs2 is empty
End If

【讨论】:

很抱歉,您能否提供解决方案的完整代码,因为在 VBA 中非常新,我似乎无法正确实施... Andre 我已按照您的建议编辑了代码,但在调试时出现以下错误:未声明 CSql 函数 我链接了这个函数,你需要把它复制到一个代码模块中。 我更新了代码,你能看一下吗? select * from query_offene_wartungspunkte where [Date] > #2020/12/10 00:00:00# And id = 2

以上是关于Basic Visual Basic 程序错误 - 可能是变量/语法错误(标记为 Fat)的主要内容,如果未能解决你的问题,请参考以下文章

无法在 Visual Basic 2010 中编译,出现错误

Visual Basic从入门到精通pdf

office 2010 提示“不信任到visual basic project“问题处理

Visual Basic SaveDialog 缺少 dll

Visual Basic.Net 2010 Exception 错误含义

编译错误:“函数或接口标记为受限或函数使用了 Visual Basic 不支持的自动化类型”[重复]