bnfkru4567
New member
- Local time
- Today, 09:27
- Joined
- Dec 2, 2021
- Messages
- 10
Dear All Experts,
I need your help into ACCESS VBA as I am facing the following problem:
Background:
In my Access file, I have a query and 3 tables.
Query was named as "run-query" (has 14 fields)
1st table was named as "source1"
2nd table was named as "source2"
3rd table was named as "result" (has 14 fields)
The first two tables are the sources and use the query "run-query" to run those sources. After run the Query, I would like to copy Query Results and paste them to < result>table.
(Remark: the name of fields in "run-query" and <result> are the same.)
I use the ACCESS VBA to run "Queries" a lot of times per day (so will not consider to use Access -Query --Append function). After each run the "query", I have to copy and paste the results to <results>table.
Problem:
Now I am facing the problem to copy and paste the Query-Results into <result> table. If the <result>table is empty(let say it is the first time to run today), my VBA code is working. However, if it is not the first time of day to run VBA (Hence: there are a few records/rows in <result>table), then it will not work. Thus, I have to keep all the query results in <result>tables for further processing.
Could you all please check the following VBA codes and tell me how to improve them further ?
Thanks in advance
Private sub btnTest_Click()
Dim db as DAO.database
Dim rs as DAO.Recordset
Dim I1 as integer
DoCmd.OpenQuery "run-query"
Docmd. Close
<remark: it works for the above first part to run Query>
Set db=CurrentDb()
DoCmd.OpenQuery "run-query", acViewNormal, acReadOnly
DoCmd. SelectObject acQuery,. "run-query"
DoCmd.RunCommand acCmdSelectAllRecords
RunCommand acCmdCopy
Set rs=db.OpenRecordset("result", dbOpenDynast)
DoCmd.OpenTable"result", acViewNormal, acEdit
On Error Resume Next
rs.MoveFirst
rs.MoveLast
I1=rs.Record.Count
If I1>1 then
rs.MoveLast
rs. AddNew
DoCmd.GoToRecord, , acNewRec
DoCmd.RunCommand acCmdPasteAppend <remark: even I used "acCmdPaste" but it is not working>
Else
DoCmd.RunCommand acCmdPasteAppend <remark: it works as <result>table has no record>
end if
rs.Update
DoCmd.Close acQuery, "run-query", acSaveNo <remark: it works>
end sub
I need your help into ACCESS VBA as I am facing the following problem:
Background:
In my Access file, I have a query and 3 tables.
Query was named as "run-query" (has 14 fields)
1st table was named as "source1"
2nd table was named as "source2"
3rd table was named as "result" (has 14 fields)
The first two tables are the sources and use the query "run-query" to run those sources. After run the Query, I would like to copy Query Results and paste them to < result>table.
(Remark: the name of fields in "run-query" and <result> are the same.)
I use the ACCESS VBA to run "Queries" a lot of times per day (so will not consider to use Access -Query --Append function). After each run the "query", I have to copy and paste the results to <results>table.
Problem:
Now I am facing the problem to copy and paste the Query-Results into <result> table. If the <result>table is empty(let say it is the first time to run today), my VBA code is working. However, if it is not the first time of day to run VBA (Hence: there are a few records/rows in <result>table), then it will not work. Thus, I have to keep all the query results in <result>tables for further processing.
Could you all please check the following VBA codes and tell me how to improve them further ?
Thanks in advance
Private sub btnTest_Click()
Dim db as DAO.database
Dim rs as DAO.Recordset
Dim I1 as integer
DoCmd.OpenQuery "run-query"
Docmd. Close
<remark: it works for the above first part to run Query>
Set db=CurrentDb()
DoCmd.OpenQuery "run-query", acViewNormal, acReadOnly
DoCmd. SelectObject acQuery,. "run-query"
DoCmd.RunCommand acCmdSelectAllRecords
RunCommand acCmdCopy
Set rs=db.OpenRecordset("result", dbOpenDynast)
DoCmd.OpenTable"result", acViewNormal, acEdit
On Error Resume Next
rs.MoveFirst
rs.MoveLast
I1=rs.Record.Count
If I1>1 then
rs.MoveLast
rs. AddNew
DoCmd.GoToRecord, , acNewRec
DoCmd.RunCommand acCmdPasteAppend <remark: even I used "acCmdPaste" but it is not working>
Else
DoCmd.RunCommand acCmdPasteAppend <remark: it works as <result>table has no record>
end if
rs.Update
DoCmd.Close acQuery, "run-query", acSaveNo <remark: it works>
end sub
Last edited: