我在MS Access 2010中通过记录集运行循环一直在苦苦挣扎。我以前能够做到这一点,但从来没有记录集引用主键。我已经解决了dbSeeChanges错误,但它仍然无法正常工作。感谢您的任何建议和感恩节快乐!
Private Sub Command26_Click()
Dim Query As String
Dim db As Database
Dim rs As Recordset
Dim url1 As String
Dim url3 As String
Dim fullurl As String
url1 = "http://arlenmusic.azurewebsites.net/access-event-musicians.php?
event_musician_id="
url3 = "&action=add"
Query = "SELECT dbo_event_musician.job_number,
dbo_event_musician.event_musician_id, dbo_event_musician.sub_event_id,
dbo_event_musician.name, dbo_event_musician.instrument_key,
dbo_event_musician.set_up_time, dbo_event_musician.start_time,
dbo_event_musician.booked_until, dbo_event_musician.special_songs,
dbo_event_musician.attire, dbo_event_musician.include_status,
dbo_event_musician.sub, dbo_event_musician.status, dbo_event_musician.notes,
dbo_event_musician.date_entered from dbo_event_musician WHERE
(((dbo_event_musician.sub_event_id) Like [Forms]![event_scheduling]!
[sub_event_selector])) ORDER BY dbo_event_musician.instrument_key;"
Set db = CurrentDb
Set rs = db.OpenRecordset(Query, , dbSeeChanges)
Do Until rs.EOF
fullurl = url1 + rs!event_musician_id + url3
Application.FollowHyperlink (fullurl)
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
答案 0 :(得分:2)
根据您的评论,问题实际上不是dbSeeChanges
。相反,您使用的是Database.OpenRecordset,但它包含表达式[Forms]![event_scheduling]![sub_event_selector]
,这对DAO来说是陌生的。
请参阅,它适用于Access,因为您正在浏览Access层,Access会识别这些表达式并为您解决它们。但是当您直接使用DAO时(例如Database.Execute
或Database.OpenRecordset
),您实际上绕过了Access,因此无法解析这些表达式。 DAO正确识别那些未解析的标识符,并为您提供了太少的参数"错误。
解决此问题的一种方法是使用QueryDef.Parameters
,然后执行OpenRecordset
。
这是一个未经测试的空气样本示例,以说明这一点。
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rs As DAO.Recordset
Set db = CurrentDb
Set qdf = db.CreateQueryDef
qdf.SQL = "PARAMETERS [MyFirstParameter] TEXT, [MySecondParameter] LONG; SELECT a, b, c FROM myTable WHERE a = [MyFirstParameter] AND b = [MySecondParameter];"
qdf.Parameters("MyFirstParameter").Value = "abc"
qdf.Parameters("MySecondParameter").Value = 2
Set rs = qdf.OpenRecordset(dbOpenDynaset, dbSeeChanges)
您可以查阅MSDN以获取更多文档here。这种方法的优点是您可以指定要参数化的内容以及如何解析,然后直接分配您最初阅读的值。这样你就可以避免使用像Forms!MyForm!MyControl
这样的脆弱表达式,这些表达式在运行时才会被验证,并且可能会产生意想不到的结果。
替代方法是将表达式包装在Eval()
中,这将允许它在两个上下文中工作但我真的不喜欢它,因为它基本上允许在SQL中执行任意代码,我更喜欢避免这样做。