代码之家  ›  专栏  ›  技术社区  ›  MStudz

从VBA查询访问权限

  •  2
  • MStudz  · 技术社区  · 6 年前

    我目前正试图在中编写一个查询 VBA 获取数据 Access .为了绳子 SQL ,除了WHERE子句之外,其他一切都正常工作,我不知道如何在语法方面编写它。我得到了一份工作

    运行时错误3075-查询中出现语法错误(缺少运算符) 表示

    下面是我的代码:

    Sub ImportFromAccess_Size()
    
        database_file_path = Range("Database_File_Path").Value
    
        Dim DbLoc As String
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim xlbook As Workbook
        Dim xlsheet As Worksheet
        Dim recCount As Long
        Dim SQL As String
    
        DbLoc = database_file_path
    
        Application.ScreenUpdating = False
    
        Set xlbook = ActiveWorkbook
        Set xlsheet = Sheets("Simulation Input Data") 
    
        Application.StatusBar = "Connecting to an external database..."
        'Application.Cursor = xlWait
    
        tool_file_path = Range("Tool_File_Path").Value
        database_file_path = Range("Database_File_Path").Value
        size_system = Range("Size_System").Value
    
        Set db = OpenDatabase(DbLoc)
        'Set db = OpenDatabase(database_file_path)
    
        SQL = "SELECT SCHED_SURGERY_DATETIME "
        SQL = SQL & "FROM Raw_Data "
        SQL = SQL & "ORDER BY SCHED_SURGERY_DATETIME "
        SQL = SQL & "WHERE [PROCEDURE_NM_WID] = (" & size_system & ")"
    
        Sheets("Simulation Input Data").Activate
    
        Set rs = db.OpenRecordset(SQL, dbOpenSnapshot)
        xlsheet.Range("A2").CopyFromRecordset rs
    
    End Sub
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   user4039065 user4039065    6 年前

    将文本字符串条件放在单引号中(也称为“ticks”)

    SQL = "SELECT [SCHED_SURGERY_DATETIME] "
    SQL = SQL & "FROM Raw_Data "
    SQL = SQL & "WHERE [PROCEDURE_NM_WID] = '" & size_system & "' "
    SQL = SQL & "ORDER BY [SCHED_SURGERY_DATETIME];"