我正在开发一个访问应用程序,我正在尝试执行以下操作: 用户将输入插入到搜索中,然后我在我的数据库中搜索,我想操纵结果(生成一个html文件并将结果放在那里)。
所以我有一个带有一个输入的表单,用户插入了他想要搜索的内容。
然后重定向到另一个表单,并使用以下on_load代码:
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的数组,而不是将其打印给用户。
可能吗?
答案 0 :(得分:2)
你应该用你的查询打开一个Recordset,循环它的结果,并在循环中应用你的逻辑。
例如,您可以在以下地址之后拨打我的潜水员:
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
答案 1 :(得分:1)
这样的东西可以让你将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
答案 2 :(得分:0)
使用相同的SQL打开记录集:
SELECT * FROM results WHERE (condition)
并根据需要浏览记录。
答案 3 :(得分:0)
阅读Recordset类。您可以像这样使用它来将查询结果导入对象:
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM results WHERE (condition)", dbOpenDynaset, dbFailOnError + dbSeeChanges)
从那里你可以运行它,查询和操纵它。