使用 MS Access 下载缓慢
Slow Downloads Using MS Access
我有一个子程序可以打开 excel,创建到我的 google sheet 的连接,然后使用插入语句将数据添加到我的 Access 数据库。
这有效但速度非常慢(需要 ~30 秒才能获得 6 条记录)
Private Sub ImportFromGoogleSheet()
On Error GoTo ErrHandler
Dim appXL As Object 'Excel.Application
Dim wbk As Object 'Excel.Workbook
Dim wst As Object 'Excel.Worksheet
Dim Timer As Integer
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True 'If you want to see the excel sheet - enable this row (good for debugging)
Set wbk = appXL.Workbooks.Add
Set wst = wbk.Worksheets(1)
With wst
.QueryTables.Add Connection:= _
"URL;https://connection to site here ", Destination:=.Range("$A")
.Name = "Worksheet1"
.QueryTables(1).Refresh
End With
'Wait for google-doc data to be downloaded.
Timer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And Timer < 40
'Sleep 250 ' Wait 0.25 sec before re-checking data
'Timer = Timer + 1
Loop
Dim rownum As Integer
rownum = 4
wst.cells(rownum, 2).Select
Do While (wst.cells(rownum, 2).Value <> "")
Dim sqlStr As String
Dim ts, dol As Date
Dim sn, lt As String
Dim nod As Integer
ts = wst.cells(rownum, 2).Value
dol = wst.cells(rownum, 5).Value
sn = wst.cells(rownum, 3).Value
lt = wst.cells(rownum, 4).Value
nod = wst.cells(rownum, 6).Value
sqlStr = "INSERT INTO table VALUES"
DoCmd.SetWarnings False
DoCmd.RunSQL sqlStr
DoCmd.SetWarnings True
rownum = rownum + 1
Loop
wbk.Close SaveChanges:=False 'Don't save excel sheet
appXL.Quit
Exit Sub
ErrHandler:
If (Err.Number = 3022) Then
Debug.Print "Record Already Exists"
Resume
End If
Debug.Print Err.Description & Err.Number
wbk.Close SaveChanges:=False 'Don't save excel sheet
appXL.Quit
End Sub
我已经尝试删除第一个 while 循环,但随后没有导入任何内容
代码中是否有某些东西减慢了它的速度,或者这只是一个缓慢的过程?
*我知道这可能适合代码审查,但我更感兴趣的是为什么它慢而不是它的错误代码
**编辑以添加调试输出和新的 while
Debug.Print "before wait while " & Now
'Wait for google-doc data to be downloaded.
lTimer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And lTimer < 40
Debug.Print "Wait loop " & lTimer
Debug.Print "during wait while " & Now
Sleep 250 ' Wait 0.25 sec before re-checking data
DoEvents
lTimer = lTimer + 1
Loop
Debug.Print "after wait while" & Now
代码结束
start time 18/07/2017 9:06:58 a.m.
before connect 18/07/2017 9:06:58 a.m.
before wait while 18/07/2017 9:07:00 a.m.
Wait loop 0
during wait while 18/07/2017 9:07:00 a.m.
Wait loop 1
during wait while 18/07/2017 9:07:00 a.m.
Wait loop 2
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 3
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 4
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 5
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 6
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 7
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 8
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 9
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 10
during wait while 18/07/2017 9:07:03 a.m.
Wait loop 11
during wait while 18/07/2017 9:07:03 a.m.
after wait while 18/07/2017 9:07:28 a.m.
使用Debug.Print
查看哪些部分实际花费了这么长时间。 How to debug VBA code
Timer
是Access中的函数,请勿将其用作变量名。
您的等待循环必须有一个 Sleep
调用和一个 DoEvents
调用以避免占用您的 CPU。
Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
'Wait for google-doc data to be downloaded.
lTimer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And lTimer < 40
Debug.Print "Wait loop " & lTimer ' Ctrl+g shows output
Sleep 250 ' Wait 0.25 sec before re-checking data
DoEvents
lTimer = lTimer + 1
Loop
如果这没有帮助,您可能不得不忍受它。
发现另一个问题,即仅下载了 100 条记录,该问题的解决方案也解决了速度问题。
我找到了解决方案 here。
它说您需要将 link 的格式从
更改为
https://docs.google.com/spreadsheets/d/YOURSPREADSHEET/edit?usp=sharing
至
https://spreadsheets.google.com/tq?tqx=out:html&tq=&key=YOURSPREADSHEET&gid=1
将其从可编辑视图更改为更基本的视图
我有一个子程序可以打开 excel,创建到我的 google sheet 的连接,然后使用插入语句将数据添加到我的 Access 数据库。
这有效但速度非常慢(需要 ~30 秒才能获得 6 条记录)
Private Sub ImportFromGoogleSheet()
On Error GoTo ErrHandler
Dim appXL As Object 'Excel.Application
Dim wbk As Object 'Excel.Workbook
Dim wst As Object 'Excel.Worksheet
Dim Timer As Integer
Set appXL = CreateObject("Excel.Application")
appXL.Visible = True 'If you want to see the excel sheet - enable this row (good for debugging)
Set wbk = appXL.Workbooks.Add
Set wst = wbk.Worksheets(1)
With wst
.QueryTables.Add Connection:= _
"URL;https://connection to site here ", Destination:=.Range("$A")
.Name = "Worksheet1"
.QueryTables(1).Refresh
End With
'Wait for google-doc data to be downloaded.
Timer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And Timer < 40
'Sleep 250 ' Wait 0.25 sec before re-checking data
'Timer = Timer + 1
Loop
Dim rownum As Integer
rownum = 4
wst.cells(rownum, 2).Select
Do While (wst.cells(rownum, 2).Value <> "")
Dim sqlStr As String
Dim ts, dol As Date
Dim sn, lt As String
Dim nod As Integer
ts = wst.cells(rownum, 2).Value
dol = wst.cells(rownum, 5).Value
sn = wst.cells(rownum, 3).Value
lt = wst.cells(rownum, 4).Value
nod = wst.cells(rownum, 6).Value
sqlStr = "INSERT INTO table VALUES"
DoCmd.SetWarnings False
DoCmd.RunSQL sqlStr
DoCmd.SetWarnings True
rownum = rownum + 1
Loop
wbk.Close SaveChanges:=False 'Don't save excel sheet
appXL.Quit
Exit Sub
ErrHandler:
If (Err.Number = 3022) Then
Debug.Print "Record Already Exists"
Resume
End If
Debug.Print Err.Description & Err.Number
wbk.Close SaveChanges:=False 'Don't save excel sheet
appXL.Quit
End Sub
我已经尝试删除第一个 while 循环,但随后没有导入任何内容
代码中是否有某些东西减慢了它的速度,或者这只是一个缓慢的过程?
*我知道这可能适合代码审查,但我更感兴趣的是为什么它慢而不是它的错误代码
**编辑以添加调试输出和新的 while
Debug.Print "before wait while " & Now
'Wait for google-doc data to be downloaded.
lTimer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And lTimer < 40
Debug.Print "Wait loop " & lTimer
Debug.Print "during wait while " & Now
Sleep 250 ' Wait 0.25 sec before re-checking data
DoEvents
lTimer = lTimer + 1
Loop
Debug.Print "after wait while" & Now
代码结束
start time 18/07/2017 9:06:58 a.m.
before connect 18/07/2017 9:06:58 a.m.
before wait while 18/07/2017 9:07:00 a.m.
Wait loop 0
during wait while 18/07/2017 9:07:00 a.m.
Wait loop 1
during wait while 18/07/2017 9:07:00 a.m.
Wait loop 2
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 3
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 4
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 5
during wait while 18/07/2017 9:07:01 a.m.
Wait loop 6
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 7
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 8
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 9
during wait while 18/07/2017 9:07:02 a.m.
Wait loop 10
during wait while 18/07/2017 9:07:03 a.m.
Wait loop 11
during wait while 18/07/2017 9:07:03 a.m.
after wait while 18/07/2017 9:07:28 a.m.
使用Debug.Print
查看哪些部分实际花费了这么长时间。 How to debug VBA code
Timer
是Access中的函数,请勿将其用作变量名。
您的等待循环必须有一个 Sleep
调用和一个 DoEvents
调用以避免占用您的 CPU。
Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
'Wait for google-doc data to be downloaded.
lTimer = 0
Do While Left(wst.cells(1, 1), 12) = "ExternalData" And lTimer < 40
Debug.Print "Wait loop " & lTimer ' Ctrl+g shows output
Sleep 250 ' Wait 0.25 sec before re-checking data
DoEvents
lTimer = lTimer + 1
Loop
如果这没有帮助,您可能不得不忍受它。
发现另一个问题,即仅下载了 100 条记录,该问题的解决方案也解决了速度问题。
我找到了解决方案 here。
它说您需要将 link 的格式从
更改为
https://docs.google.com/spreadsheets/d/YOURSPREADSHEET/edit?usp=sharing
至
https://spreadsheets.google.com/tq?tqx=out:html&tq=&key=YOURSPREADSHEET&gid=1
将其从可编辑视图更改为更基本的视图