vba multiple for loops

travisdh

Registered User.
Local time
Today, 03:01
Joined
Jul 5, 2010
Messages
64
Hi All,

I have a system i am working on which will hopefully let me automatically generate subsamples of parent samples based on if they are selected, through the sampleaction query, which basically filters only the selected samples for processing.

The sample table contains a whole lot of different fields, but basically the main ones are SampleID (autoID), ClientSampleID(Text) and ParentSampleID. For a normal sample the user would never see the ParentSampleID field, however if we recieve a sample from the user which we need to use to generate other samples, i generate subsamples, which are new samples to the system, but the ParentSampleID of the subsamples references back to the main sample recieved by the client.

Anyways i have a form which shows a datasheet with the checked samples so the user can see which samples will have action undertaken, and on the same form there are a series of checkboxes, which let the user make a subsample of that type for each selected checkbox, and a text box beside that which lets the user enter the number of subsamples of that type of sample to be generated, and a short description which is applied to all samples.

I thought the best way to approach this was multiple loops, so a loop through the recordset of the sampleaction, and then for each record in sampleaction, to do a series of if loops, so if the checkbox for Carbon is checked, then look at how many samples for carbon are required, and then make a loop to insert however many carbon tubes are required inserting the projectID of the parent sample, the parentsample's SampleID in the child samples ParentSampleID field, and a description consisting of along the lines of "Tube Type & Number (of loop, eg Carbon Tube 7, Carbon Tube 6, Carbon Tube 8) and the description.

For some reason it only partly inserts data, for the first sample on the first loop it inserts the parentsampleID, and the projectID but that is it, and on other samples it completly ignores it.

Can anyone take a peak and give me any comments or suggestions?


Code:
Dim rs As DAO.Recordset
Dim db As Database
Dim idsample As Integer
Dim carbon As Integer
Dim xad As Integer
Dim silica As Integer
Dim other As Integer
Dim sql As String
Dim clientsampleid As String
Dim proj As Integer
Dim desctxt As String
 

sql = "INSERT INTO SampleRegister (ClientSampleID,ParentSampleID,Project) VALUES " & "(" & "'" & clientsampleid & "'" & "," & idsample & "," & proj & ");"
Set db = CurrentDb
Set rs = db.OpenRecordset("SampleAction")

Do While Not rs.EOF
   idsample = rs!sampleID
   proj = rs!OrderID
   
           If IsNull([carbon_chk]) = False Then
            
            For carbon = 1 To [ct_txt]
           clientsampleid = "Carbon Tube " & carbon & ", " & "," & Val(Me.desc_txt)
            DoCmd.RunSQL (sql)
            Next carbon
        
        Else
        End If
   
        If IsNull([xad_chk]) = False Then
        
            For xad = 1 To [xad_txt]
            clientsampleid = "XAD-2 Tube " & xad & ", " & "," & Val(Me.desc_txt)
            DoCmd.RunSQL (sql)
            Next xad
        
        Else
        End If
        
        If IsNull([silica_chk]) = False Then
        
            For silica = 1 To [Silica_txt]
            clientsampleid = "Silica Tube " & silica & ", " & "," & Val(Me.desc_txt)
            DoCmd.RunSQL (sql)
            Next silica
        
        
        Else
        End If
        
        If IsNull([other_chk]) = False Then
        
            For other = 1 To [other_txt]
            clientsampleid = "'" & [othername_txt] & "'" & " , " & other & ", " & "," & Val(Me.desc_txt)
            DoCmd.RunSQL (sql)
            Next other
        
        
        Else
        End If
        
        
   
   rs.MoveNext
Loop
End Sub
 
This is a simple logical error. You are setting the value of your SQL statement before you set the values in your variables. I initially posted an example of how you should do it but I then realized that it still contained a logical error. I'm going to have to reread your post to see what I can recommend to solve this problem.
 
I've read through your post and your code and it's still not very clear to me what you're trying to do.

What is "SampleAction"? Is that a table? Is it the same table your datasheet form is bound to?

Here it looks like you are referencing a value on your form:
If IsNull([carbon_chk]) = False Then

I'm assuming you really want the value out of the recordset? You should then be using:
If IsNull(rs!carbon) = False Then


I guess I'm still very baffled as to what you're trying to achieve. If this was properly designed I'm not sure you'd need to use any code at all to create your records. Can you make your database file available?
 
Sorry if i did not explain it well.

I have a sampleregister table which has all of the sample information, and then a checkbox for selected, and a textbox for selectedby. When the checkbox is selected the selectedby text in the table is updated with the current windows username.

The sampleaction is simply a query that returns the sampleID, selected checkbox, and selectedby textbox of samples that are selected by the currently logged in windows user.

What i was trying to achieve was allow the end user to select the records they want to make subsamples from, and click a button on the mainform which pops up a subform. The subform has a series of checkboxes for what subsample type the user wants to generate, and how many and a description box, so that the idea is that the user checks the box beside which type of subsample they want (e.g. Carbon and Silica) and picks how many subsamples they want (e.g 3 for carbon, 1 for Silica and enters a generic description (e.g. 24 hour emissions) and it loops through the selected sampleID's and generates new samples (subsamples) that relate to each of the selected samples in SampleAction.

The SampleRegister table is where i was inserting records for the subsamples, and basically the idea was that the SampleID is an autoID so that is filled out by access, the Project is the project from the sampleAction table, and the ParentSampleID is taken from the SampleID of the SampleAction query for the current record in the loop.


People had discussed why not create a seperate subsample table, which would make sense, however i have other tables which hold results, and the results can reference either a subsample, or the sample supplied from the client, so it was easier to put in the sample table a field for parentsampleID which just gets the sampleID of the sample this subsample was made for, and then the results table can still link in well.

Hopefully that makes sense?
 
the db is 30mb, but i can delete the example samples (76000 of them) and just leave a few in.
 
i have attached the test db, excuse the messyness, since its still very much in devel i have not fixed all the tables, but there is a form called ForayJobEntry, basically if you click on that and scroll across a few projects you will see samples in the sample table, if you select a few, and click on Generate Samples the subform should pop up and give you more options of generating samples and the likes.
 

Attachments

Users who are viewing this thread

Back
Top Bottom