Hi
I have been following this thread and I tried Paolo's method and
Douglas'recommendation but either way I m getting run-time errors. What am
I
doing wrong ?
I have 2 select queries that I want to run in afterUpdate of a control in
a
form.
My query names:BGVHDBL,BGVHSGL
So I tried using RunSQL first as follow
Private Sub File_Ref_AfterUpdate()
DoCmd.SetWarnings False
If Me.Dirty Then
Me.Dirty = False
DoCmd.RunSQL ("BGVHSGL")
End If
End Sub
and I m getting Run-time error '3129'
Invalid SQL statement; expected'Delete,'insert','select'or 'update'
Then I tried
Private Sub File_Ref_AfterUpdate()
Dim dbCurr As DAO.Database
Dim sglSQL As String
DoCmd.SetWarnings False
If Me.Dirty Then
Me.Dirty = False
sglSQL = "BGVHSGL"
dbCurr.Execute sglSQL, dbFailOnError
End If
End Sub
and I m getting Run-Time error '91':
Object variable or with block variable not set
I have not written SQL in VBA before that is why I m trying to just use
the
query name. If it may help to see the SQL view of one of the queries:
SELECT Sum(IIf(BDTP.bed="sgl",1,0)) AS countsgl, MNST.[File Reference],
MNST.travelling, RMMT.Room, BDTP.bed
FROM BDTP INNER JOIN (RMMT INNER JOIN MNST ON RMMT.rmmtid = MNST.Roomate)
ON
BDTP.ID = RMMT.Bed
GROUP BY MNST.[File Reference], MNST.travelling, RMMT.Room, BDTP.bed
HAVING (((MNST.[File Reference])=[Forms]![BGVCH]![File Ref]) AND
((MNST.travelling)=True) AND ((BDTP.bed)="sgl"));
What corrections do I need to make to have those 2 queries running ?
Many thanks.
Douglas J. Steele said:
In my opinion, it's better to use the Execute method of the Database
object
than to use RunSQL.
The reason for this is that the Execute method will raise an trappable
error
that can help if something goes wrong (plus it doesn't require you to
change
the SetWarnings setting)
Dim dbCurr As DAO.Database
Dim strSQL As String
Set dbCurr = CurrentDb()
strSQL = "DROP TABLE tbl_Insertion"
dbCurr.Execute strSQL, dbFailOnError
strSQL = "UPDATE tbl_FORM_CA_BSI_Events SET " & _
"DateOfEvent2 = Format([DateOfEvent],'yy/mm')"
dbCurr.Execute strSQL, dbFailOnError
strSQL = "ALTER TABLE tbl_Form_CA_BSI_Events ADD COLUMN " & _
"DateOfEvent2 text(6)"
dbCurr.Execute strSQL, dbFailOnError
Set dbCurr = Nothing
The reason I prefer using a string variable to hold the SQL, rather than
simply putting it as part of the command as in
dbCurr.Execute "DROP TABLE tbl_Insertion", dbFailOnError
is that when you're doing more complicated SQL, it's often useful to be
able
to print out the contents of the string to help troubleshoot problems.
--
Doug Steele, Microsoft Access MVP
(no e-mails, please!)
Hi john lawlor,
docmd.setwarnings false ''to switch off system messages
eg1
docmd.runsql("DROP TABLE tbl_insertion")
eg2
docmd.runsql("UPDATE tbl_FORM_CA_BSI_Events SET " _ ''to continue the
line
on another line
& "DateOfEvent2 = Format([DateOfEvent],'yy/mm')")
eg3
docmd.runsql("ALTER TABLE tbl_Form_CA_BSI_Events ADD COLUMN " _ &
"DateOfEvent2 text(6)")
HTH Paolo
:
This is my first foray into VBA.
I want to run many queries (which work) in a VBA module.
e.g. 1
DROP TABLE tbl_insertion, tbl_maintenance, tbl_form_ca_bsi_events,
tbl_BiopatchTestFactors_FormDetails,
tbl_BiopatchTestFactors_FormNumbers,
tbl_CHGScrubTestFactor,tbl_Form_CA_BSI_Catheter,
tbl_Form_LineDaysAggregateData, tbl_Form_LineDaysAggregateInfo;
;
e.g. 2
UPDATE tbl_FORM_CA_BSI_Events SET DateOfEvent2 =
Format([DateOfEvent],"yy/mm");
e.g. 3
ALTER TABLE tbl_Form_CA_BSI_Events
ADD COLUMN DateOfEvent2 text(6);
The examples revealed in an internet search show that "" _ and other
characters are added, but I am having difficulty divinig the logic.
1. Could someone be so kind as to re-write some of the above
and
2. Suggest online sites which would explain the use of the characters.
Thank you,
John