使用可能为空vba的excel数据覆盖表中的Access值

时间:2017-04-21 16:08:09

标签: database excel vba excel-vba ms-access

我正在构建一个Excel工具,允许用户在excel中的链接表中写入数据,然后按下一个命令按钮,在Access表中找到相应的数据,并用用户输入的值替换该数据。我有一些有用的东西(见下文),但我的所有值域都必须是长文本,我不能推送任何空变量。因此,如果变量为空,我将其等于空格,例如:var =" &#34 ;.我想知道是否有办法将变量推入表中,即使变量为空,例如:var =""。

On Error GoTo fish

wbk1 = ActiveWorkbook.Name
Set sh1 = Workbooks(wbk1).Sheets(ActiveSheet.Name)
With ActiveSheet
  Dim startLoc As Variant
  For lcnt = 1 To .ListObjects.Count
    startLoc = .ListObjects(lcnt).Range.Address
    locate = Split(startLoc, "$")
    rowst = Split(locate(2), ":")
    rowst = CInt(rowst(0)) + 1
    colst = Asc(locate(1)) - 64

    lastrow = sh1.Range(locate(1) & 10000).End(xlUp).Row

    For roww = rowst To lastrow
      If sh1.Cells(roww, colst).Value <> "" Then
        actID = sh1.Cells(roww, colst).Value
        dtcr = Date
        crby = VBA.Interaction.Environ$("UserName")
        actdesc = sh1.Cells(roww, colst + 1).Value
        assgn = sh1.Cells(roww, colst + 2).Value
        If assgn = "" Then assgn = " "
        compby = sh1.Cells(roww, colst + 3).Value
        If compby = "" Then compby = " "
        compdt = sh1.Cells(roww, colst + 4).Value
        If compdt = "" Then compdt = " "
        comm = sh1.Cells(roww, colst + 5).Value
        If comm = "" Then comm = " "
        learn = sh1.Cells(roww, colst + 6).Value
        If learn = "" Then learn = " "
        appID = .ListObjects(lcnt).Name
        appID = Split(appID, "_")
        appID = appID(1)

        Set cn = CreateObject("ADODB.Connection")

        cn.Open "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=H:\Home\Bevicor\Action Items.accdb"

        ssql = Array("Update ActionItem Set ActionItemDescription ='" & actdesc 
           & "', AssignedTo ='" & assgn & "', CompleteBy ='" & compby & "', 
           CompletionDate ='" & compdt & "', Comments ='" & comm & "', 
           Learnings ='" & learn & "' Where ID =" & actID)

        cn.Execute ssql
      End If
    Next roww
  Next lcnt
End With

If adds = 0 Then
    MsgBox "No new Action Items were found. If you added action items, make 
    sure the 'DB ID' entry is empty; this will be filled automatically."
    GoTo fish
End If

With ActiveSheet
  For lcnt = 1 To .ListObjects.Count
    startLoc = .ListObjects(lcnt).Range.Address
    sh1.Range(startLoc).ListObject.QueryTable.refresh BackgroundQuery:=False
  Next lcnt
End With

fish:
If Error = True Then
    MsgBox "Update failed."
End If
cn.Close

很抱歉,如果我的代码或问题很混乱。这是我的第一个问题,我100%自学编码。

0 个答案:

没有答案