Results 1 to 6 of 6

Thread: Insert field using a loop

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    4,831

    Re: Insert field using a loop

    Hello,
    I think the correct syntax is,
    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.

  3. #3
    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
    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.

  4. #4
    Join Date
    Jan 2008
    Posts
    3,755

    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.

  5. #5
    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

  6. #6
    Join Date
    May 2008
    Posts
    4,570

    Re: Insert field using a loop

    Hello,
    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.

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work
    By mADiRAkSHii in forum Operating Systems
    Replies: 4
    Last Post: 25-01-2011, 07:23 PM
  3. Replies: 6
    Last Post: 09-07-2010, 11:26 AM
  4. Multi Field value field in Microsoft Access
    By Erubiel in forum Windows Software
    Replies: 3
    Last Post: 20-11-2009, 12:55 AM
  5. Insert Link Field crashes Word 2007 Consistently
    By hatred in forum MS Office Support
    Replies: 1
    Last Post: 02-08-2007, 05:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,112,948.52628 seconds with 17 queries