PDA

View Full Version : [SOLVED:] Opening query on loading of a form



sdhruv
06-11-2015, 08:56 PM
I am trying to run a query everytime a certain form is loaded so I made an on load event and wrote the following code:
Private Sub Form_Load()
q = db.Execute(Query2, acViewNormal, acEdit)
End Sub

It works at the start but then crashes into a run time error 424 object required.

I also tried:
Private Sub Form_Load()
q = Docmd.OpenQuery(Query2, acViewNormal, acEdit)
End Sub

It also works initially before crashing into a compile error xpected Function or expression
Please help I do not know whats wrong or if there is another way to do it.

jonh
06-12-2015, 02:28 AM
Where is db set?
You only use brackets if a function returns a value. docmd isn't a function.
Unless Query2 is a variable it needs quotes.

'open SELECT query for viewing
DoCmd.OpenQuery "query2"

'run action query/sql
DoCmd.RunSQL "query2"
CurrentDb.Execute "query2"

'recordset
Set rs = CurrentDb.OpenRecordset("query2")

sdhruv
06-12-2015, 03:14 AM
As you can see I have never coded in vba before and am a total noob at coding any language anyway, I am still getting errors but that I think I will ask my superiors for help, he says that he has written these codes before so it would be better to do what he wants. Thanks a lot though, do you know any website or video channel that can help me learn all this? I would be a bit better off if I could learn some of it when I hit a road block instead of waiting for you the whole day to save my ass

jonh
06-12-2015, 03:57 AM
No worries.
I guess this would be a good place to start Introduction to the Visual Basic Programming Language (https://msdn.microsoft.com/library/xk24xdbe%28v=vs.90%29.aspx)