Hi,
I've had a look again at the original "example" code I found:
Sub txtBB_OnExit
Dim sSQL as String
sSQL = "UPDATE tblBBB SET " & _
" [AA] = '" & Me.txtAA & "', " & _
" [BB] = '" & Me.txtBB & "'" & _
" WHERE [PkID] = " & Me.txtPkID & ";"
DoCmd.SetWarnings False
CurrentDb.Execute sSQL
DoCmd.SetWarnings True
End Sub
and apparantly "txtBB" doesn't refer to the table in the second database
that I want to update. It seems to be the name of the text box for the common
columns in both tables that I wish to update. So "txtBB" could be
"Country"(and the same for "AA"). "tblBB" then would be the name of the table
in the second database, so "ctbto table 1". I therefore tried using the
following code:
Sub Country_OnExit
Dim sSQL as String
sSQL = "UPDATE [ctbto table 1] SET " & _
" [Region] = '" & Me.chrRegion & "', " & _
" [Country] = '" & Me.chrCountry & "'" & _
" WHERE [ID] = " & Me.dtmID & ";"
DoCmd.SetWarnings False
CurrentDb.Execute sSQL
DoCmd.SetWarnings True
End Sub
However, I'm quite obviously still doing something wrong and
misunderstanding how this code is supposed to work as when I run it, it
deletes the entry on the form in the first database for the particular
country whose record I want to update, while not updating the second table.
I've also tried using the "After Update" event property and deleting the "On
Exit" event property in the code, but then I get compile errors.
Also, how do I let Access know where to find the relevant table in the
second database? I've tried linking that table but to no effect. Please bear
with me here, I'm really not very proficient with SQL and VBA programming
though I'm trying to learn them. I hope you can understand my explanation and
can tell me what I"m doing wrong.
Thanks much for your time.
Douglas J. Steele said:
What's the exact code you're using, then (since what you posted before
doesn't have the brackets around the table name)?
--
Doug Steele, Microsoft Access MVP
(no private e-mails, please)
Kevin said:
Hi, thanks for answering.
Yes, 'ctbto table 1' is the name of the 2nd table I want to update. I've
already tried putting in the brackets but I just get the same error
message
for the whole name, i.e. '[ctbto table 1]'. I've also tried putting in an
underscore between the 3 parts of the table name and I still get error
messages. What could I do?
Douglas J. Steele said:
It would appear that the name of your table is ctbto table 1, which has
blanks in it. Whenever table or field names have blanks, they must be
enclosed in square brackets:
sSQL = UPDATE [ctbto table 1] SET " & _
" [Country Code] = '" & Me.Country Code & "', " & _
"
Code:
= " & Me.code & " " & _
" WHERE [ID] = " & Me.ID & ";"
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)
Hi,
Thanks for your reply. I've looked for code to execute an Update Query
and
found the following:
"So lets assume you have two textboxes txtAA and txtBB and you want to
save
their values in a second table tblBBB. Note that I will assume that AA
is
a
String/Text and BB is a number.Lets say the Primary key used in the
form
and
table tblBBB is called [PkID] in tblBBB and txtPkID on the form
Sub txtBB_OnExit
Dim sSQL as String
sSQL = UPDATE tblBBB SET " & _
" [AA] = '" & Me.txtAA & "', " & _
" [BB] = " & Me.txtBB & " " & _
" WHERE [PkID] = " & Me.txtPkID & ";"
DoCmd.SetWarnings False
CurrenDb.Execute sSQL
DoCmd.SetWarnings True
End Sub
You will need to rebuild this based on you actual fields and textbox
names."
I've rebuilt the code in the following way:
Dim sSQL As String
Sub ctbto table 1_OnExit
sSQL = UPDATE ctbto table 1 SET " & _
" [Country Code] = '" & Me.Country Code & "', " & _
" [code] = " & Me.code & " " & _
" WHERE [ID] = " & Me.ID & ";"
DoCmd.SetWarnings False
CurrenDb.Execute sSQL
DoCmd.SetWarnings True
End Sub
However, when I enter the name of the 2nd table (ctbto table 1) I get
an
'Expected: End of Statement' error message.
I've tried to contact the person who wrote this code, but do you know
why
I'm getting this error message and how I could correct it?
Thanks much in advance.
:
Kevin wrote:
Thanks for your reply! On one of the databases (the one I'm
building,
the other was done by another person) I actually do updates with
forms. I've done this using a command button which uses a macro to
open a form which I further filter using an SQL statement. So if I'm
looking a record for say "Canada" i have command button which when
clicked on opens a form for "Canada" where editing of that record
can
be done (I've locked the main form already so no editing can be done
there) I'm not very profficient with code though. Would you be able
to suggest code to me that I could use to update 2 or even more
databases using forms?
The simplest method would involve using the AfterUpdate event of your
form
to execute an update query against the other table. That update could
use
the record just saved to update the other table.
[/QUOTE][/QUOTE]
[/QUOTE]