|
-
Jul 19th, 2007, 10:47 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Problem adding multiple records
EDIT: Should have posted in Database forum, so if admin can change please
VB6 and Access 2002
Can anybody see why the following code only adds the first entry.
Say, for example I add 4 records to the DB, all four records are added, but all with the same values as the first record.
I haven't used this method of adding data before, so am I missing something obvious?
Code:
Set cn = New ADODB.Connection
cn.ConnectionString = strConnection & App.Path & "\Membership.mdb"
cn.Open
Dim adoCommand As ADODB.Command
Set adoCommand = New ADODB.Command
For i = 0 To ctr - 1
iMonth = CInt(cmbMM(i).Text)
dtDate = CDate(cmbDD(i).Text & "/" & cmbMM(i).Text & "/" & cmbYY(i).Text)
strDay = cmbDay(i).Text
strType = cmbType(i).Text
strVenue = cmbVenue(i).Text
strTimes = cmbTime1(i).Text & " to " & cmbTime2(i).Text & " - " & cmbTime3(i).Text & " to " & cmbTime4(i).Text
strYear = cmbYY(i).Text
strSQL = "INSERT INTO tbl_CourseList (Course_Month, Course_Date, " & _
"Course_Day, Course_Type, " & _
"Course_Venue, Course_Times, Course_Year)" & _
" Values (iMonth, dtDate, strDay, strType, strVenue, strtimes, strYear)"
With adoCommand
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = strSQL
.Prepared = True
.Parameters.Append .CreateParameter(, adInteger, adParamInput, 50, iMonth)
.Parameters.Append .CreateParameter(, adDate, adParamInput, 50, dtDate)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strDay)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strType)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strVenue)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strTimes)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strYear)
.Execute , , adCmdText + adExecuteNoRecords
'note: the last two arguments used for Execute here makes the execution of the command faster
End With
Next
cn.Close
Set cn = Nothing
Also, what do I set the adoCommand to at the end to tidy it up?
Last edited by aikidokid; Jul 19th, 2007 at 10:48 AM.
Reason: Posted in wrong forum
-
Jul 19th, 2007, 10:50 AM
#2
Re: Problem adding multiple records
Put a break on this line
Code:
For i = 0 To ctr - 1
and step through the code.
I have a feeling the control array index may not be getting updated.
-
Jul 19th, 2007, 10:54 AM
#3
Thread Starter
Frenzied Member
Re: Problem adding multiple records
 Originally Posted by Hack
Put a break on this line
Code:
For i = 0 To ctr - 1
and step through the code.
I have a feeling the control array index may not be getting updated.
In my code I do have
Code:
For i = 0 To ctr - 1
'my code
Next
But yes I have stepped through and the values are different!
-
Jul 19th, 2007, 10:58 AM
#4
Thread Starter
Frenzied Member
Re: Problem adding multiple records
Just stepped through again, adding two more records.
They were both added with the two different sets of values, but are saved in the DB with the same values as the first record!
-
Jul 19th, 2007, 10:59 AM
#5
Re: Problem adding multiple records
Do a Debug.Print for each control in the array. If in fact they are getting changed, you should see 4 different sets of data.
If this is so, do a debug.print on your SQL statement and see what is actually being sent back to the database.
-
Jul 19th, 2007, 11:10 AM
#6
Thread Starter
Frenzied Member
Re: Problem adding multiple records
These are the Debug.Print results of me adding two more records to the DB (reverse order)
INSERT INTO tbl_CourseList (Course_Month, Course_Date, Course_Day, Course_Type, Course_Venue, Course_Times, Course_Year) Values (iMonth, dtDate, strDay, strType, strVenue, strtimes, strYear)
2007
10.00 to 10.00 - 10.00 to 10.00
HQ
3rd Dan Grading
Sunday
06/11/2007
11
INSERT INTO tbl_CourseList (Course_Month, Course_Date, Course_Day, Course_Type, Course_Venue, Course_Times, Course_Year) Values (iMonth, dtDate, strDay, strType, strVenue, strtimes, strYear)
2007
09.00 to 09.00 - 09.00 to 09.00
Clifton
2nd Dan Grading
Saturday
05/11/2007
11
-
Jul 19th, 2007, 11:20 AM
#7
Re: Problem adding multiple records
Ok. So they are different so you aren't adding the same record twice, right? Isn't that the question?
-
Jul 19th, 2007, 11:25 AM
#8
Thread Starter
Frenzied Member
Re: Problem adding multiple records
 Originally Posted by Hack
Ok. So they are different so you aren't adding the same record twice, right? Isn't that the question?
Sort of, but if you look below, see what has been saved into the database:
Course_ID Course_Month Course_Date Course_Day Course_Type Course_Venue Course_Times Course_Year
38 11 05/11/2007 Saturday 2nd Dan Grading Clifton 09.00 to 09.00 - 09.00 to 09.00 2007
39 11 05/11/2007 Saturday 2nd Dan Grading Clifton 09.00 to 09.00 - 09.00 to 09.00 2007
Exactly the same values as only the first record added
-
Jul 19th, 2007, 11:43 AM
#9
Re: Problem adding multiple records
That doesn't make any sense. If your INSERT has the right values, then those values should be what is getting back to the database.
Put a break right after the .Execute and send it through a multi-record insert loop.
After each insert look at the new record in the table.
Keep the debug.print in place for the insert.
Tell me what you see in the table after each individual insert.
-
Jul 19th, 2007, 11:53 AM
#10
Thread Starter
Frenzied Member
Re: Problem adding multiple records
 Originally Posted by Hack
That doesn't make any sense. If your INSERT has the right values, then those values should be what is getting back to the database.
Put a break right after the .Execute and send it through a multi-record insert loop.
After each insert look at the new record in the table.
Keep the debug.print in place for the insert.
Tell me what you see in the table after each individual insert.
Thanks Hack
I will have to do this tomorrow, but I will definately post back.
-
Jul 19th, 2007, 11:54 AM
#11
Re: Problem adding multiple records
I'll be looking for it. This just doesn't make any sense. See you tomorrow!
-
Jul 19th, 2007, 03:47 PM
#12
Re: Problem adding multiple records
Why aren't you just doing a cn.Execute on strSQL?
The most difficult part of developing a program is understanding the problem.
The second most difficult part is deciding how you're going to solve the problem.
Actually writing the program (translating your solution into some computer language) is the easiest part.
Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.
Please Help Us To Save Ana
-
Jul 19th, 2007, 07:30 PM
#13
New Member
Re: Problem adding multiple records
I agree with you A142! instead of strSQL = .... why not cn.Execute "insert into..."
-
Jul 20th, 2007, 03:01 AM
#14
Thread Starter
Frenzied Member
Re: Problem adding multiple records
I was looking in the FAQ's and this was the method advised to me by one of the mods.
Even so, I am still intrigued as to why I am having this problem - it shouldn't happen!
If I can find out why then it helps for the future if it happens again.
-
Jul 20th, 2007, 06:33 AM
#15
Re: Problem adding multiple records
 Originally Posted by Hack
That doesn't make any sense. If your INSERT has the right values, then those values should be what is getting back to the database.
Put a break right after the .Execute and send it through a multi-record insert loop.
After each insert look at the new record in the table.
Keep the debug.print in place for the insert.
Tell me what you see in the table after each individual insert.
Did you try this yet?
-
Jul 20th, 2007, 06:52 AM
#16
Re: Problem adding multiple records
Code:
strSQL = "INSERT INTO tbl_CourseList (Course_Month, Course_Date, " & _
"Course_Day, Course_Type, " & _
"Course_Venue, Course_Times, Course_Year)" & _
" Values (?, ?, ?, ?, ?, ?, ?)"
Your not clearing the parameters collection.
I'm surprised you didn't get errors... are you using ON ERROR RESUME NEXT?
-
Jul 20th, 2007, 08:56 AM
#17
Thread Starter
Frenzied Member
Re: Problem adding multiple records
 Originally Posted by leinad31
Code:
strSQL = "INSERT INTO tbl_CourseList (Course_Month, Course_Date, " & _
"Course_Day, Course_Type, " & _
"Course_Venue, Course_Times, Course_Year)" & _
" Values (?, ?, ?, ?, ?, ?, ?)"
Your not clearing the parameters collection.
I'm surprised you didn't get errors... are you using ON ERROR RESUME NEXT?
@Hack
No, hopefully in the next hour or so I will have time.
@leinad31
As I said earlier, I haven't used this method before.
You are saying replace the variables with question marks and just use the variables with the parameter values?
BTW I am not using On Error Resume Next
Last edited by aikidokid; Jul 20th, 2007 at 09:07 AM.
-
Jul 20th, 2007, 09:10 AM
#18
Re: Problem adding multiple records
I'm surprised you aren't getting errors either.... each time you append a parameter, you're adding a new one... the OLD ones are still there....
which is why the same values keep getting put in there.... solution is simple... re-arrange the code.
Code:
Set cn = New ADODB.Connection
cn.ConnectionString = strConnection & App.Path & "\Membership.mdb"
cn.Open
Dim adoCommand As ADODB.Command
Set adoCommand = New ADODB.Command
strSQL = "INSERT INTO tbl_CourseList (Course_Month, Course_Date, " & _
"Course_Day, Course_Type, " & _
"Course_Venue, Course_Times, Course_Year)" & _
" Values (?, ?, ?, ?, ?, ?, ?)"
With adoCommand
.ActiveConnection = cn
.CommandType = adCmdText
.CommandText = strSQL
.Prepared = True
.Parameters.Append .CreateParameter(, adInteger, adParamInput, 50)
.Parameters.Append .CreateParameter(, adDate, adParamInput, 50)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50)
.Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50)
End With
For i = 0 To ctr - 1
iMonth = CInt(cmbMM(i).Text)
dtDate = CDate(cmbDD(i).Text & "/" & cmbMM(i).Text & "/" & cmbYY(i).Text)
strDay = cmbDay(i).Text
strType = cmbType(i).Text
strVenue = cmbVenue(i).Text
strTimes = cmbTime1(i).Text & " to " & cmbTime2(i).Text & " - " & cmbTime3(i).Text & " to " & cmbTime4(i).Text
strYear = cmbYY(i).Text
With adoCommand
.Parameters(0).Value = iMonth
.Parameters(1).Value = dtDate
.Parameters(2).Value = strDay
.Parameters(3).Value = strType
.Parameters(4).Value = strVenue
.Parameters(5).Value = strTimes
.Parameters(6).Value = strYear
.Execute , , adCmdText + adExecuteNoRecords
'note: the last two arguments used for Execute here makes the execution of the command faster
End With
Next
cn.Close
Set cn = Nothing
-tg
-
Jul 20th, 2007, 09:12 AM
#19
Thread Starter
Frenzied Member
Re: Problem adding multiple records
leinad31, I have just tried this with the question marks in place of the variables, as you showed and I still get the same problem of the first record being added for both entries I added.
I stepped through the whole process and looked at each value as it was passed and they were different.
-
Jul 20th, 2007, 09:40 AM
#20
Thread Starter
Frenzied Member
Re: Problem adding multiple records
@ techgnome
This works - thanks very much 
Just so I understand the process now in place ...
- Set up the SQL statement - using question marks for the placeholder of the value to be passed
- Create the parameters first.
- Loop through, changing the values to be passed to the parameters.
Also, do I need to set the adoCommand to nothing and close it?
-
Jul 20th, 2007, 10:06 AM
#21
Re: Problem adding multiple records
set command to nothing, close the connection, set connection to nothing.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|