Insert with VBA

Hi all,

I've just inserted a new field into an already existent table, and I would like to populate this field which spans over two thousand records. I would like to create a vba script which will allow me to do this.

I'm assuming that I would have to create a new module but I'm not quite sure how to go about it. I am familiar with attaching simple code to buttons etc, but I'm stumped as to how to do this!

grateful for advice.

- My new field is FileCode, the table name is Files and the vale I want to insert is the letter A -
I have tried the following:

Public Sub insert()
Dim db As Database
Set db = CurrentDb()
CurrentDb.Execute "INSERT INTO Files (FileCode) VALUES ("A")"
db.Close
End Sub

and

DoCmd.RunSQL "INSERT INTO Files ([FileCode]) "VALUES ('A')"

PipMicAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
UPDATE Files SET FileCode = "A"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
PipMic,

Your code is using "INSERT INTO ... " which is an Append query. This adds new records. It does not modify existing records.

As matthewspatrick pointed out you need to use an Update query to modify existing records.

0
PipMicAuthor Commented:
Hi all,

Thanks, but what would be the VBA code?


0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

PipMicAuthor Commented:
I'm completely lost..... how can i use an append query to update a new field with the same value??
0
PipMicAuthor Commented:
Easy after all
0
PipMicAuthor Commented:
Ok soory  i was confusing append with query....thanks all.
0
PipMicAuthor Commented:
soorry confusing append with update query!!!
0
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
<< ...but what would be the VBA code?>>

Your VBA code is fine. It is the SQL Statement that is the issue. The "INSERT INTO Files (FileCode) VALUES ("A")" is the part that needs changed.

<<m completely lost..... how can i use an append query to update a new field with the same value??>>

You don't. An append query is used to add records. You use an Update query to update a field in an existing record.

Public Sub insert()

     CurrentDb.Execute "UPDATE Files SET FileCode = ""A"";"

End Sub

Open in new window

and

DoCmd.RunSQL "UPDATE Files SET FileCode = ""A"";"

Open in new window


0
Boyd (HiTechCoach) Trimmell, Microsoft Access MVPDesigner and DeveloperCommented:
You're welcome. Glad I could assist.
0
PipMicAuthor Commented:
Thanks HiTech,

For the VBA correction.

:)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.