我是VBA的新手,正在编写一个宏,该宏根据用户输入的月末日期从Access下载数据。
当前,我在获取要从Access下载的信息时遇到问题。
更具体地说:当我运行宏并输入月底日期时,没有下载任何数据,但是我也没有收到错误消息。
我一直在尝试不同的代码迭代以使其正常工作。在我收到各种错误消息之前;现在我没有收到错误消息,但是没有数据下载。
我正在在线阅读内容,我需要使用一个函数来从输入框(box
)中获取变量以在Access查询中工作。我想知道是否与我的功能设置有关。还是我的Access查询有问题。
Sub Expense_Download()
'
' Expense_Download Macro
'
Dim cnn As ADODB.Connection, rs As ADODB.Recordset, sQRY As String, strFilePath As String, box As Variant, myvar As Variant
strFilePath = "C:\Users\NVanWyk\Documents\Fidato\Lunch And Learn\Lunch and Learn Access DB\Nate's Expenses DB.accdb" 'Replace the ‘DatabaseFolder’ and ‘myDB.accdb’ with your DB path and DB name
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
box = InputBox("What month do you want expense data for? Please use the MM/DD/YYYY format.", "Expense Month?")
If Not IsDate(box) Then MsgBox "Value entered is not a valid date, please try again.", , "Input Value is Not Correct"
End
myvar = box
cnn.Open "provider = microsoft.ace.oledb.12.0;" & _
"Data Source=" & strFilePath & ";"
'have to indent the data source or the code does not run
sQRY = "SELECT * FROM Expenses Where Expenses.Expense_Month=myvar()" 'Replace ‘tblData’ with your Access DB Table name or Query name from which you want to download the data"
rs.CursorLocation = adUseClient
rs.Open myvar(), sQRY, cnn, adOpenStatic, adLockReadOnly
Application.ScreenUpdating = False
Sheet1.Range("A2").CopyFromRecordset rs
'sheet 1 for whatever reason still pastes to the "expense" sheet
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub
答案 0 :(得分:0)
首先,仅包含单词“ End”的行将停止执行。
如果您想在用户输入错误的情况下停止执行,请执行以下操作:
[{rbc,<0.2067.0>,800},{bmo,<0.2068.0>,700},{ing,<0.2069.0>,200},0]
您将用户的月份输入存储在myvar变量中,但实际上并没有放入选择查询中。
代替此:
If Not IsDate(box) Then
MsgBox "Value entered is not a valid date, please try again.", , "Input Value is Not Correct"
End 'or Exit Sub
End If
尝试一下:
sQRY = "SELECT * FROM Expenses Where Expenses.Expense_Month=myvar()"
但是,在InputBox中期望的值并不明显。我假设它是一个介于1到12之间的数字,并且Expense_Month字段在您的数据库中也是一个数字。 如果它也以YYYY-MM格式包含年份并且是varchar,则需要像这样更改代码:
sQRY = "SELECT * FROM Expenses Where Expenses.Expense_Month= " & myvar
以上任一情况均成立,IsDate不是检查输入是否正确的正确函数。例如,如果用户在框中输入20,它将返回True,因为它是一个日期,但是1900年1月20日。
此外,从此行中删除第一个参数 myvar()。
sQRY = "SELECT * FROM Expenses Where Expenses.Expense_Month= '" & myvar & "'"
根据您的其他输入,我建议使用YYYY-MM-DD作为用户输入。因此,您的查询将定义为:
rs.Open myvar(), sQRY, cnn, adOpenStatic, adLockReadOnly
rs.Open sQRY, cnn, adOpenStatic, adLockReadOnly
要测试您的查询,请在VB编辑器中打开“本地”窗口,一旦执行到此点,请查看sQRY的值是什么,然后在Access的SQL编辑器中运行该值以查看它是否将正常工作。
答案 1 :(得分:0)
感谢大家的帮助!
我在上面进行了编辑,并进行了一些其他研究,并能够使代码正常工作。我认为遇到的障碍是因为我使用日期(MM/DD/YYYY
)作为变量,所以我需要在查询中添加#
标记,以便Access将变量识别为日期。
Sub Expense_Download()
'
' Expense_Download Macro
'
Dim cnn As ADODB.Connection, rs As ADODB.Recordset, sQRY As String, strFilePath As String, box As Variant, myvar As Variant
strFilePath = "C:\Users\NVanWyk\Documents\Fidato\Lunch And Learn\Lunch and Learn Access DB\Nate's Expenses DB.accdb" 'Replace the ‘DatabaseFolder’ and ‘myDB.accdb’ with your DB path and DB name
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
box = InputBox("What month do you want expense data for? Please use the MM/DD/YYYY format.", "Expense Month?")
If Not IsDate(box) Then
MsgBox "Value entered is not a valid date, please try again.", , "Input Value is Not Correct"
End
End If
myvar = box
cnn.Open "provider = microsoft.ace.oledb.12.0;" & _
"Data Source=" & strFilePath & ";"
'have to indent the data source or the code does not run
sQRY = "SELECT * FROM Expenses Where Expenses.Expense_Month= #" & myvar & "#" 'Replace ‘tblData’ with your Access DB Table name or Query name from which you want to download the data"
' access query
' # before and after the "myvar" variable are necessary for access to recognize the variable as a date and to run the query successfully.
rs.CursorLocation = adUseClient
rs.Open sQRY, cnn, adOpenStatic, adLockReadOnly
Application.ScreenUpdating = False
Sheet1.Range("A2").CopyFromRecordset rs
'sheet 1 for whatever reason still pastes to the "expense" sheet
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
Exit Sub
End Sub