0

I am working on a employee DB in which there is a training form. I have over 2000 employees who need trainings from time to time. I need to create batches of predetermined number (i.e. 50) of employees in that form. Also, I need to create them for each new training. So trainings are linked to TrainingID. I have created a for loop statement just as the following:

Dim StartC As Integer
Dim EndC As Integer

Set StartC = Nz(DMax("BatchNo", "T25TrnBatch", "T25TrnBatch.TrnID= " & Forms!F13NewTraining!TrnId), 0) + 1
Set EndC = Me.TBatchtxt

StrSQL = "INSERT INTO T25TrnBatch(TrnID,BatchNo) VALUES((Forms!F13NewTraining!TrnId),StartC);"

For BCounter = StartC To EndC
    DoCmd.SetWarnings False
    DoCmd.RunSQL StrSQL
    DoCmd.SetWarnings True
Next BCounter

(Note: TBatchtxt is the total batches need to be created)

But when I run it a pop up appears saying StartC.

Also I want the result to like the following:

BatchNo TrnID

1 101

2 101

3 101

Please HELP. Thanks.

2 Answers 2

2

The SQL statement that you are executing doesn't "know" what values you are trying to insert - you must concatenate the values into the SQL string. As a tip for debugging, you could place a Debug.Print StrSQL on the line after you have concatenated the values in to see what it actually is.

Also, the SQL statement is not inside the loop, so the values will never change. Conversely, there is no need to change SetWarnings inside the loop - you can just change it outside the loop.

Dim StartC As Integer
Dim EndC As Integer

Set StartC = Nz(DMax("BatchNo", "T25TrnBatch", "T25TrnBatch.TrnID= " & Forms!F13NewTraining!TrnId), 0) + 1
Set EndC = Me.TBatchtxt


DoCmd.SetWarnings False
For BCounter = StartC To EndC
    StrSQL = "INSERT INTO T25TrnBatch(TrnID,BatchNo) " _   
        & " VALUES(" & Forms!F13NewTraining!TrnId & "," & BCounter & ");"
    DoCmd.RunSQL StrSQL
Next BCounter
DoCmd.SetWarnings True
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but it doesn't work. The Batch no doesn't increase.
Ooops - the SQL statement used StartC as in your original code. I've modified it to use BCounter which is being incremented by the loop.
That's even better. Thanks again.
0

Ok, I have figured it out. I inserted the DMax function again in the SQL. It is as follows:

Dim BCounter As Integer '''I forgot to mention this line in my question
Dim StartC As Integer
Dim EndC As Integer

StartC = Nz(DMax("BatchNo", "T25TrnBatch", "T25TrnBatch.TrnID= " & Forms!F13NewTraining!TrnId), 0) + 1
EndC = Me.TBatchtxt


DoCmd.SetWarnings False
For BCounter = StartC To EndC
StrSQL = "INSERT INTO Table1Test(TID,BID) " _
    & " VALUES(" & Forms!TestForm1!TIDtxt & "," & Nz(DMax("BatchNo", "T25TrnBatch", "T25TrnBatch.TrnID= " & Forms!F13NewTraining!TrnId), 0) + 1 & ");"
DoCmd.RunSQL StrSQL
Next BCounter
DoCmd.SetWarnings True

Thanks @Applecore for pointing me in the right direction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.