Ms access 访问vba-获取代码中的结果
我正在开发access应用程序,并尝试执行以下操作: 用户将输入插入到搜索中,然后我在数据库中搜索,我想处理结果(生成一个html文件并将结果放入其中) 所以我有一个表单,有一个输入,在那里用户插入他想要搜索的东西 然后它重定向到另一个表单,加载代码如下:Ms access 访问vba-获取代码中的结果,ms-access,vba,Ms Access,Vba,我正在开发access应用程序,并尝试执行以下操作: 用户将输入插入到搜索中,然后我在数据库中搜索,我想处理结果(生成一个html文件并将结果放入其中) 所以我有一个表单,有一个输入,在那里用户插入他想要搜索的东西 然后它重定向到另一个表单,加载代码如下: Private sub form_load() dim str as string set frm = screen.activeForm 'gets the last form str = frm!search 't
Private sub form_load()
dim str as string
set frm = screen.activeForm 'gets the last form
str = frm!search 'the input the user entered
task = "SELECT * FROM results WHERE (condition)" 'some query with the db and the input
Me.recordSource = taks
end sub
此表单获取结果并打印它们
现在,我在表格上看到了结果。但是,我想要的是:获取结果并在代码中操作它们,例如,使用所有结果ID创建一个数组,而不是将其打印给用户
这可能吗?只需使用相同的SQL打开一个记录集:
SELECT * FROM results WHERE (condition)
然后根据需要浏览记录。只需使用相同的SQL打开一个记录集:
SELECT * FROM results WHERE (condition)
并根据需要浏览记录。类似的功能将允许您将vba连接到数据库并将数据取出:
Sub vbaRecords()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim SQLstr As String
Set db=CurrentDb
SQLstr = "SELECT * FROM results WHERE (condition)" ' You'll need to flesh this out to have the same condition as you've used previously.
Set rst = db.OpenRecordset(SQLstr)
' Then you can move around the recordset. Assuming you want to start at the beginning:
rst.MoveFirst
' Then you can access individual items
vbitem1 = rst!item1
' You can also loop through the different records, if there's more than 1 (your condition can narrow this down)
do until rst.EOF
' Grab items from each record in here and do something with them
rst.MoveNext
Loop
' Then close and end the connections
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
End sub
类似这样的操作将允许您将vba连接到数据库,并将数据取出:
Sub vbaRecords()
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim SQLstr As String
Set db=CurrentDb
SQLstr = "SELECT * FROM results WHERE (condition)" ' You'll need to flesh this out to have the same condition as you've used previously.
Set rst = db.OpenRecordset(SQLstr)
' Then you can move around the recordset. Assuming you want to start at the beginning:
rst.MoveFirst
' Then you can access individual items
vbitem1 = rst!item1
' You can also loop through the different records, if there's more than 1 (your condition can narrow this down)
do until rst.EOF
' Grab items from each record in here and do something with them
rst.MoveNext
Loop
' Then close and end the connections
rst.Close
db.Close
Set rst = Nothing
Set db = Nothing
End sub
仔细阅读Recordset类。您可以这样使用它将查询结果放入对象中:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM results WHERE (condition)", dbOpenDynaset, dbFailOnError + dbSeeChanges)
从那里,您可以运行它,查询和操作它,如您所愿。阅读Recordset类。您可以这样使用它将查询结果放入对象中:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM results WHERE (condition)", dbOpenDynaset, dbFailOnError + dbSeeChanges)
从那里你可以运行它,查询和操作它,你喜欢。你应该用你的查询打开一个记录集,循环它的结果,并在循环中应用你的逻辑 例如,您可以在完成以下操作后调用我的sub:
Me.recordSource = taks
For_Instance taks ' Call my sub and pass it your SQL instruction
这里有一个问题,你在你的表格中呼出并包含你的逻辑:
Private Sub For_Instance(strSQL As String)
Dim DB As dao.Database
Dim RST As dao.Recordset
Dim lngID As Long
Dim strMyField As String
Dim lngCount As Long
Dim i As Long
Set DB = CurrentDb
Set RST = DB.OpenRecordset(strSQL)
If RST.BOF Then Exit Sub ' no records found, stop.
' If you want to know how many record you have prior to loop, do:
RST.MoveLast
lngCount = RST.RecordCount
Debug.Print "There are " & lngCount & " to process."
' Let's loop on your recordset now...
' first, reposition on first record:
RST.MoveFirst
' Then start to loop
While Not RST.EOF
' This is where you do your stuff with the records
' You can grab the data that is in the current line of you recordset like this:
' RST!name_of_the_field
' name_of_the_field refers to your column names
' Suppose you have a column named ID with type long, to get the current ID, do:
lngID = RST!ID
' Suppose you have a column named MyField with type string
strMyField = RST!MyField
' and do whatever you want
' And finally you go to the next record and continue your stuff
RST.MoveNext
i = i + 1
Wend
' When you arrive here, you have processed all your records
MsgBox "All done, I have processed " & i & " records"
'Close your recordset
RST.Close
'Clean your objects
Set RST = Nothing
Set DB = Nothing
End Sub
您应该打开一个包含查询的记录集,循环查询结果,并在循环中应用您的逻辑 例如,您可以在完成以下操作后调用我的sub:
Me.recordSource = taks
For_Instance taks ' Call my sub and pass it your SQL instruction
这里有一个问题,你在你的表格中呼出并包含你的逻辑:
Private Sub For_Instance(strSQL As String)
Dim DB As dao.Database
Dim RST As dao.Recordset
Dim lngID As Long
Dim strMyField As String
Dim lngCount As Long
Dim i As Long
Set DB = CurrentDb
Set RST = DB.OpenRecordset(strSQL)
If RST.BOF Then Exit Sub ' no records found, stop.
' If you want to know how many record you have prior to loop, do:
RST.MoveLast
lngCount = RST.RecordCount
Debug.Print "There are " & lngCount & " to process."
' Let's loop on your recordset now...
' first, reposition on first record:
RST.MoveFirst
' Then start to loop
While Not RST.EOF
' This is where you do your stuff with the records
' You can grab the data that is in the current line of you recordset like this:
' RST!name_of_the_field
' name_of_the_field refers to your column names
' Suppose you have a column named ID with type long, to get the current ID, do:
lngID = RST!ID
' Suppose you have a column named MyField with type string
strMyField = RST!MyField
' and do whatever you want
' And finally you go to the next record and continue your stuff
RST.MoveNext
i = i + 1
Wend
' When you arrive here, you have processed all your records
MsgBox "All done, I have processed " & i & " records"
'Close your recordset
RST.Close
'Clean your objects
Set RST = Nothing
Set DB = Nothing
End Sub
我有点糊涂,有更具体的解释吗?我有点糊涂,有更具体的解释吗?