Updated query via VBA

J

JohnB762

Hello to everybody,
I am trying to update a form with an update query by a clic on a button and
get the number of record updated, passing the parameter from a form named
"Esercizi_pubblici_Update".
In this form there are:
- Old text box "Attivita" to a new Combo called "CboAttività"
- Old text box "Esercizio" to a new Combo called "CboEsercizio"


The table I would like to update is "Esercizi_pubblici" with his [Attività] &
[Tipologia_Esercizio] fileds.

The SQL of the update query is
UPDATE Esercizi_pubblici SET Esercizi_pubblici.Attività = [Forms]!
[Esercizi_pubblici_Update]![CboAttività], Esercizi_pubblici.
Tipologia_Esercizio = [Forms]![Esercizi_pubblici_Update]![CboEsercizio]
WHERE (((Esercizi_pubblici.Attività)=[Forms]![Esercizi_pubblici_Update]!
[Attivita]) AND ((Esercizi_pubblici.Tipologia_Esercizio)=[Forms]!
[Esercizi_pubblici_Update]![Esercizio]));
and if I run the query alone everything is fine.
But when I tried via VBA doesn't work at all.



The code that I am trying, run always with a syntax error message and since
my knowledge with VBA/SQL is limited I kindly aks help.
Many thanks and Kind Regards
John

Private Sub Update_Click()
On Error GoTo Err_Update_Click

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strOffice As String
Dim strOffice1 As String

Set db = CurrentDb
Set qdf = db.QueryDefs("Esercizi_pubblici_Update")

strOffice = "='" & Me.CboAttività.Value & "' "
strOffice1 = "='" & Me.CboEsercizio.Value & "' "

strSQL = "UPDATE Esercizi_pubblici.*" & _
"SET Esercizi_pubblici.[Attività]= strOffice, Esercizi_pubblici.
[Tipologia_Esercizio]=strOffice1" & _
"WHERE Esercizi_pubblici.[Attività] = Esercizi_pubblici.[Attivita]
" & _
"And Esercizi_pubblici.[Tipologia.Esercizio]= Esercizi_publici.
[Esercizio]"
qdf.SQL = strSQL

MsgBox "You updated " & qdf.RecordsAffected & " records", vbOKOnly,
"Pubblici Esercizi"
Set qdf = Nothing
Set db = Nothing

End Sub
 
M

Marshall Barton

JohnB762 said:
I am trying to update a form with an update query by a clic on a button and
get the number of record updated, passing the parameter from a form named
"Esercizi_pubblici_Update".
In this form there are:
- Old text box "Attivita" to a new Combo called "CboAttività"
- Old text box "Esercizio" to a new Combo called "CboEsercizio"


The table I would like to update is "Esercizi_pubblici" with his [Attività] &
[Tipologia_Esercizio] fileds.

The SQL of the update query is
UPDATE Esercizi_pubblici SET Esercizi_pubblici.Attività = [Forms]!
[Esercizi_pubblici_Update]![CboAttività], Esercizi_pubblici.
Tipologia_Esercizio = [Forms]![Esercizi_pubblici_Update]![CboEsercizio]
WHERE (((Esercizi_pubblici.Attività)=[Forms]![Esercizi_pubblici_Update]!
[Attivita]) AND ((Esercizi_pubblici.Tipologia_Esercizio)=[Forms]!
[Esercizi_pubblici_Update]![Esercizio]));
and if I run the query alone everything is fine.
But when I tried via VBA doesn't work at all.



The code that I am trying, run always with a syntax error message and since
my knowledge with VBA/SQL is limited I kindly aks help.
Many thanks and Kind Regards
John

Private Sub Update_Click()
On Error GoTo Err_Update_Click

Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Dim strOffice As String
Dim strOffice1 As String

Set db = CurrentDb
Set qdf = db.QueryDefs("Esercizi_pubblici_Update")

strOffice = "='" & Me.CboAttività.Value & "' "
strOffice1 = "='" & Me.CboEsercizio.Value & "' "

strSQL = "UPDATE Esercizi_pubblici.*" & _
"SET Esercizi_pubblici.[Attività]= strOffice, Esercizi_pubblici.
[Tipologia_Esercizio]=strOffice1" & _
"WHERE Esercizi_pubblici.[Attività] = Esercizi_pubblici.[Attivita]
" & _
"And Esercizi_pubblici.[Tipologia.Esercizio]= Esercizi_publici.
[Esercizio]"
qdf.SQL = strSQL

MsgBox "You updated " & qdf.RecordsAffected & " records", vbOKOnly,
"Pubblici Esercizi"
Set qdf = Nothing
Set db = Nothing

End Sub


I can see a couple of things in your code. One is that you
need to plug in the value of the variables, not their names.

The other is that you only set the querydef's SQL property,
you never run the query.

Try something more like:

Private Sub Update_Click()
On Error GoTo Err_Update_Click

Dim db As DAO.Database
Dim strSQL As String

Set db = CurrentDb

strSQL = "UPDATE Esercizi_pubblici " & _
" SET[Attività]=" & Me.CboAttività & _
",[Tipologia_Esercizio]=" & Me.CboEsercizio & _
" WHERE [Attività] = [Attivita] " & _
" And [Tipologia.Esercizio] = [Esercizio]"

db.Execute strSQL
MsgBox "You updated " & db.RecordsAffected & " records", _
vbOKOnly, "Pubblici Esercizi"
Set db = Nothing
End Sub

Alternatively, there really is no need to construct the SQL
string in code when you have the saved parameter query
already set up. This is the way I would do it:

Private Sub Update_Click()
On Error GoTo Err_Update_Click

Dim db As DAO.Database
Dim qdf As QueryDef
Dim prm As Parameter

Set db = CurrentDb()
Set qdf = db.QueryDefs("Esercizi_pubblici_Update")

For each prm In qdf.Parameters
prm.Value = Eval(prm.Name)
Next prm

qdf.Execute strSQL
MsgBox "You updated " & qdf.RecordsAffected & " records", _
vbOKOnly, "Pubblici Esercizi"
Set qdf = Nothing
Set db = Nothing
End Sub
 
J

JohnB762 via AccessMonster.com

Sir, thank you very much for your help. It works fine.
Thanks again.
Regards
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top