INSERT INTO with Loop problem!

Aragan

New member
Local time
Today, 01:16
Joined
Jan 3, 2012
Messages
5
Hi, i have a problem qith access 2007... the code works but it adds one extra row to the table dont know why...

Code:
Private Sub Guardar_Click()
Dim num20 As Integer
 
num20 = Me.Testo33
For i = 1 To num20
DoCmd.SetWarnings False
DoCmd.RunSQL "INSERT  INTO Contratos ([Nombre Agente],[Nombre  Cliente],DNI,[Nombre  Empresa],CIF,[Telefono Fijo 1], [Telefono Fijo 2],  [Telefono Movil],  Tarifa, [Fecha WE Entrega], [Fecha WE Pago], [Fecha  Daily]) VALUES ('" & Me.Nombre_Agente & "','" &  Me.Nombre_Cliente & "','" & Me.DNI & "','" &  Me.Nombre_Empresa & "','" & Me.CIF & "','" &  Me.Telefono_Fijo_1 & "','" & Me.Telefono_Fijo_2 & "','"  & Me.Telefono_Movil & "','2.0','" & Me.Fecha_WE_Entrega  & "','" & Me.Fecha_WE_Pago & "','" & Me.Fecha_Daily  & "')"
DoCmd.SetWarnings True
Next i
Me.Requery
End Sub

For example if num20 is "4" the code inserts 5 rows.. but in the 1st one the field "Tarifa" is empty. Tarifa is the only field that im passing a string instead of a parameter from the form...
If num20 is 0 or negative it inserts 1 row with "Tarifa" empty ,could someone explain me why? thx

Edit: If i remove everything! except the insert into it stills inserting 2 rows, and one with "tarifa" empty, whyyyyyyyy? :s like

Code:
Private Sub Guardar_Click()
DoCmd.RunSQL "INSERT  INTO Contratos ([Nombre Agente],[Nombre   Cliente],DNI,[Nombre  Empresa],CIF,[Telefono Fijo 1], [Telefono Fijo 2],   [Telefono Movil],  Tarifa, [Fecha WE Entrega], [Fecha WE Pago], [Fecha   Daily]) VALUES ('" & Me.Nombre_Agente & "','" &   Me.Nombre_Cliente & "','" & Me.DNI & "','" &   Me.Nombre_Empresa & "','" & Me.CIF & "','" &   Me.Telefono_Fijo_1 & "','" & Me.Telefono_Fijo_2 & "','"   & Me.Telefono_Movil & "','2.0','" & Me.Fecha_WE_Entrega   & "','" & Me.Fecha_WE_Pago & "','" & Me.Fecha_Daily   & "')"
End Sub
 
Last edited:
Is the form on which the button is located bound to the Contratos table?
 
Is the form on which the button is located bound to the Contratos table?

Yes it is, i created the form with the "auto create form" tool selecting the contratos table, in the Contratos Form appears all the fields from the Contratos table and after that i created the button
 
Last edited:
Typically you would use a bound form to add a new record (one at a time), but you can also add records via an append query but that is typically done with an unbound form. If you comment out the code entirely, will the form enter a new record with the information you want?


From your code it looks like you want to add multiple identical records based on the information that is entered in the form. Is that correct? If so, why would you append a series of records with all of the same data?
 
Typically you would use a bound form to add a new record (one at a time), but you can also add records via an append query but that is typically done with an unbound form. If you comment out the code entirely, will the form enter a new record with the information you want?


From your code it looks like you want to add multiple identical records based on the information that is entered in the form. Is that correct? If so, why would you append a series of records with all of the same data?

Is not the same data because i also created 7 textboxes that contains what kind of "Tarifa" that contract have... si maybe i will have 50 contracts with Tarifa = 2.0 and another 20 with Tarifa = 3.0, and i would like to insert all of them with 1 click, they are different also because i dont put all the fields of the Contratcs Table to the Contract form... there are another 5 fields that are hidden in the form and that i will modify in the future directly in the table or using the form 1 by 1... so at the end.. all the contratcs will be different

If you comment out the code entirely, will the form enter a new record with the information you want?

It does when i close the form o.o

If the solution is a non bounded form... i already made a query and a button that runs the query and i see the results of the query but they are not being added to the table i want !
 
Last edited:
It sounds like an unbound for is what you will need.

If the solution is a non bounded form... i already made a query and a button that runs the query and i see the results of the query but they are not being added to the table i want !

Can you post the SQL text of the query?
 
It sounds like an unbound for is what you will need.



Can you post the SQL text of the query?

I made a mistake... i think is working now... but i have tried only with 2 fields... i will finish the unbounded form with all the fields and then ill tell you if i have any problem! thanks a LOT for making me see that i need an unbounded form! its been 2 days searching on forums >_<
 
You are welcome and please post back if you encounter problems.
 
You are welcome and please post back if you encounter problems.

At the end im using the SAME form, the SAME code... but i just deleted the bound between the table, form and form fields... and now is working perfectly!! THANKS!
 
Glad to hear that everything is working now!
 

Users who are viewing this thread

Back
Top Bottom