当前位置:  开发笔记 > 数据库 > 正文

VBA-SQL UPDATE/INSERT/SELECT到/从Excel工作表

如何解决《VBA-SQLUPDATE/INSERT/SELECT到/从Excel工作表》经验,为你挑选了0个好方法。

简而言之:我正在为我的客户端制作一个调度程序,由于受到限制,它需要在一个excel文件中(尽可能小).因此,一个工作表作为UI工作,其他任何工作表将是表或设置.

我正在尝试使用SQL(我是新手)来处理单个工作表(名为"TblEmpDays")上的计划数据.所以我需要在此工作表中添加/更新和检索记录.我能够获得一个SELECT查询来处理一些任意数据(并粘贴到一个范围).但是,我无法上班INSERTUPDATE上班.我看到它的结构为INSERT INTO [

$] () VALUES ();.但是这在INSERT INTO语句中给出了运行时错误" '-2147217900(80040e14)'语法错误. "

我正在使用VBA来编写所有这些,并且我创建了一个SQL帮助程序类,以使查询执行更容易.

为了澄清,我的问题是:我如何构建INSERTUPDATE查询?我错过了什么?我想尽可能多地发布相关信息,所以如果我错过任何内容,请告诉我.

类SQL:

Private pCn ' As Database
Private pResult 'As Recordset
Private pSqlStr As String

Public Property Get Result()
    Result = pResult
End Property

Public Function Init()
    Set pCn = CreateObject("ADODB.Connection")

    With pCn
        .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                            "Data Source=" & ThisWorkbook.FullName & ";" & _
                            "Extended Properties=""Excel 12.0 Macro;HDR=YES;ReadOnly=False"";"
        .Open
    End With

End Function

Public Function Cleanup()
    If Not (pCn Is Nothing) Then
        pCn.Close
        Set pCn = Nothing
    End If

    If Not pResult Is Nothing Then
        Set pResult = Nothing
    End If

End Function

Public Function CopyResultToRange(rg As Range)
    If Not rg Is Nothing And Not pResult Is Nothing Then
        rg.CopyFromRecordset pResult
    End If
End Function

Public Property Get query() As String
    query = pSqlStr
End Property
Public Property Let query(value As String)
    pSqlStr = value
End Property

Public Function Execute(Optional sqlQuery As String)
    If sqlQuery = "" Then
        sqlQuery = query
    End If

    If Not pCn Is Nothing Then
        Set pResult = pCn.Execute(sqlQuery, , CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
    Else
        MsgBox "SQL connection not established"
    End If

End Function

执行功能:

Dim s As SQL ' this is the SQL class '
Dim tbl As String
' rcDay=date string, rcIn & rcOut = time strings, rcVac=boolean string, rcSls=number string'
Dim rcName As String, rcDay As String, rcIn As String, rcOut As String, rcVac As String, rcSls As String
Dim qry As String

tbl = "[TblEmpDays$]"
qry = "INSERT INTO  (name, date, in, out, vac, sales)" & vbNewLine & _
        "VALUES ('', '', '', '', '', );"

' Set rc* vars '

s.Init
s.query = Replace(Replace(Replace(Replace(Replace(Replace(Replace(qry, _
                                    "", tbl), _
                                    "", rcSls), _
                                    "", rcVac), _
                                    "", rcOut), _
                                    "", rcIn), _
                                    "", rcDay), _
                                    "", rcName)
MsgBox s.query
s.Execute
s.Cleanup

我看了一遍都找不到解决办法.我确定我没有搜索过正确的短语或简单的东西.

推荐阅读
LEEstarmmmmm
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有