SQL Insert Statement Not Working in Access

I have the below statement which executes without an error but the value does not write into the table.  Am I missing something?

Function WriteTransaction()

Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sSql As String

Dim intPaymentID As Integer
Dim intMemberID As Integer

intPaymentID = [Form_Payment SubForm_Add].txtPaymentID_Trans.Value
intMemberID = [Form_Payment SubForm_Add].txtMemberID_Trans.Value

Set db = CurrentDb()

sSql = "Select * from [tblJunction_MemberID2PaymentID]"

Set rs = db.OpenRecordset(sSql)

sSql = "INSERT INTO tblJunction_MemberID2PaymentID (MemberID, PaymentID) VALUES (" & intMemberID & " , " & intPaymentID & ")"

db.Execute (sSql)





End Function
marku24Asked:
Who is Participating?
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.

chaauCommented:
You need to convert your int values to string:

sSql = "INSERT INTO tblJunction_MemberID2PaymentID (MemberID, PaymentID) VALUES (" & CStr(intMemberID) & " , " & CStr(intPaymentID) & ")"

Open in new window

0
marku24Author Commented:
It still didn't work.  it is very strange.
0
chaauCommented:
And what's the error? Can you show us the table structure. I suspect there could be some other fields that do not allow nulls, or one of the fields you are trying to insert is an AutoNumber.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

marku24Author Commented:
there are only 2 fields in the table I am writing to.  Both are number fields (Integer).  I can type the values into the field and the table accepts them.  This is the first time I am using Access 2013, could it be that?
0
chaauCommented:
And what is the error?
0
mbizupCommented:
Give this a try instead... you really only need the sSQL string and the Execute statement for what you are doing.  I added "dbFailOnError" to the execute statement so that you WILL get a meaningful error if something is wrong with your SQL statement.  Without dbFailOnError, that execute statement would run without error despite problems in the SQL... (it just wouldn't write anything to the table).


Function WriteTransaction()

Dim db As DAO.Database
Dim sSql As String

Dim intPaymentID As Long
Dim intMemberID As Long

intPaymentID = [Form_Payment SubForm_Add].txtPaymentID_Trans.Value
intMemberID = [Form_Payment SubForm_Add].txtMemberID_Trans.Value

Set db = CurrentDb()

sSql = "INSERT INTO tblJunction_MemberID2PaymentID (MemberID, PaymentID) VALUES (" & intMemberID & " , " & intPaymentID & ")"

db.Execute sSQL, dbFailOnError

End Function

Open in new window

0
mbizupCommented:
A couple of corrections:


Function WriteTransaction()

Dim db As DAO.Database
Dim sSql As String

Dim intPaymentID As Long
Dim intMemberID As Long

[Form_Payment SubForm_Add].Refresh
intPaymentID = [Form_Payment SubForm_Add].txtPaymentID_Trans.Value
intMemberID = [Form_Payment SubForm_Add].txtMemberID_Trans.Value

Set db = CurrentDb()

sSql = "INSERT INTO tblJunction_MemberID2PaymentID (MemberID, PaymentID) VALUES (" & intMemberID & " , " & intPaymentID & ")"

db.Execute sSQL, dbFailOnError

End Function

Open in new window


Also, add the following line to the top of your code module, right under Option Compare Database:

Option Explicit

Open in new window

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
marku24Author Commented:
worked awesome - Thanks
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.