Go Back   TechArena Community > Software > Windows Software
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



Insert field using a loop

Windows Software


Reply
 
Thread Tools Search this Thread
  #1  
Old 22-01-2010
Member
 
Join Date: Dec 2009
Posts: 296
Insert field using a loop

Hello
I have a small problem and I can not find the solution for it, yet everything seems correct to me in my code and my program. I have a form that helps me to create a switch (X port), when I create a new switch, I want to create Access to all ports of the switch in one of the tables. The number of ports is defined depends on the information into the form. Everything seems to work except that I did not create the ports and I do not understand why it is not working. Here I am trying to insert fields using a loop.
Code:
Private Sub Commande26_Click()
 
Sun cn
    
    cn = 0
    
   Do While cn <= Me.Nombre_de_ports.Value
    
Sun tr
Sun es

tst = "INSERT INTO [T_PORT.N ° Switch] VALUES ( '"
tst = tst & N ° _Switch.Value & "')"

es = "INSERT INTO [T_PORT.Port] VALUES ( '"
es = cn & & es "');"


    cn = cn + 1
    Loop
    
    End

Last edited by Gunner 1 : 22-01-2010 at 01:52 PM.
Reply With Quote
  #2  
Old 22-01-2010
Macarenas's Avatar
Member
 
Join Date: May 2008
Posts: 4,810
Re: Insert field using a loop

Hello,
I think the correct syntax is,
Quote:
INSERT INTO [TABLE] ( [field], [field], ...) VALUES( [worth], [worth], ...)
This is the general syntax of the query, the numbers of fields and values must be identical. I do not know why they are not in your case. But it should work if they are identical. I think the code which you have posted in incomplete, if you post the full code it will be better for our understanding.
Reply With Quote
  #3  
Old 22-01-2010
Member
 
Join Date: Dec 2009
Posts: 296
Re: Insert field using a loop

Hello
Well I tested many things but I still have the same error flashing
Quote:
Access tells me that a semicolon missing from the end of my SQL statement
I think it is possible that it comes from DoCmd.RunSQL and its syntax.
Here is my code
Code:
Private Sub Commande21_Click()
We Error GoTo Err_Commande21_Click

    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord,, acMenuVer70
    
Sun swn
swn = Me.N° _swnitch.Value
 
Sun cp
    
    cp = 1
    
   Do While cp <= Me.Nombre_de_ports.Value
       
DoCmd.RunSQL "INSERT INTO T_PORT (Port) VALUES ( '" & & Cpte " ') WHERE [No. swnitch.Value] = & & swn"";"
  
    cp cp = + 1
    Loop    
    End
    
Exit_Commande21_Click:
    Exit Sub

Err_Commande21_Click:
    MsgBox Err.Description
    Resume Exit_Commande21_Click
    
End Sub

Last edited by Gunner 1 : 22-01-2010 at 01:45 PM.
Reply With Quote
  #4  
Old 22-01-2010
Sam.D's Avatar
Member
 
Join Date: Jan 2008
Posts: 3,735
Re: Insert field using a loop

Hello,
In the first code you missing a quotation mark in your RunSQL
Code:
Private Sub Commande21_Click()
We Error GoTo Err_Commande21_Click
  DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord,, acMenuVer70
Sun swn
swn = Me.N° _swnitch.Value
Sun cp    
    cp = 1    
   Do While cp <= Me.Nombre_de_ports.Value       
DoCmd.RunSQL "INSERT INTO T_PORT (Port) VALUES ( '" & & Cpte " ') WHERE [No. swnitch.Value] =" swn & & ";"
  
    cp cp = + 1
    Loop
    End
Exit_Commande21_Click:
    Exit Sub

Err_Commande21_Click:
    MsgBox Err.Description
    Resume Exit_Commande21_Click
    
End Sub
When you perform a RunSQL you need nt bracket, similar to as you do a msgbox.
Reply With Quote
  #5  
Old 22-01-2010
Member
 
Join Date: Dec 2009
Posts: 296
Re: Insert field using a loop

Hello,
This is my modified code, I have corrected it as you have mentioned in the above post.
Code:
Private Sub Commande21_Click()
We Error GoTo Err_Commande21_Click

    DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord,, acMenuVer70
  
Sun swn
swn = Me.N° _swnitch.Value
 
Sun cp
    
    cp = 1
    
   Do While cp <= Me.same.Value
       
DoCmd.RunSQL "INSERT INTO T_PORT (Port) VALUES ( '" & & Cpte " ') WHERE [No. swnitch.Value] =" swn & & ";"
  
    cp cp = + 1
    Loop   
    End
Exit_Commande21_Click:
    Exit Sub
Err_Commande21_Click:
    MsgBox Err.Description
    Resume Exit_Commande21_Click
    
End Sub
Reply With Quote
  #6  
Old 22-01-2010
Glenny's Avatar
Member
 
Join Date: May 2008
Posts: 4,550
Re: Insert field using a loop

Hello,
Quote:
DoCmd.RunSQL "INSERT INTO T_PORT (Port) VALUES ( '" & cp & "') WHERE [No. Switch.Value] =" & Swn & ";"
if cp is an integer it should not put a " ' ". whereas if Swn is a String, you should type the variables required by it. I think there is a problem in the above statement. I am not sure but something is missing from the syntax.
Reply With Quote
Reply

  TechArena Community > Software > Windows Software


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Insert field using a loop"
Thread Thread Starter Forum Replies Last Post
Watercooling: Single loop or Dual Loop Akolekar Hardware Peripherals 3 21-10-2011 11:52 PM
Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work mADiRAkSHii Operating Systems 4 25-01-2011 07:23 PM
How do I insert a signature field that replicates itself in Acrobat 9? Lakshmibanta Windows Software 6 09-07-2010 12:26 PM
Multi Field value field in Microsoft Access Erubiel Windows Software 3 20-11-2009 12:55 AM
Insert Link Field crashes Word 2007 Consistently funnybroad MS Office Support 1 02-08-2007 06:20 AM


All times are GMT +5.5. The time now is 05:11 PM.