Results 1 to 14 of 14

Thread: [RESOLVED] SQL Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Resolved [RESOLVED] SQL Question

    I have some SQL code that will work if I put it in a query in MS Access, however, it will not work if I try to do it in VB6 as a string. Can anyone help me with this; I'd be greatly appreciative.

    VB Code:
    1. '*INSERT()************************************************************************
    2. 'NAME: INSERT()
    3. 'DESC:  Inserts data from the TEST table and the PICKSL table into the MAIN table
    4. Private Sub insert()
    5.     Dim sql As String
    6.    
    7.     Set newConnection = New ADODB.connection
    8.     newConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    9.         "Data Source= C:\Documents and Settings\p6b0438\My Documents\CycleCount Test\CycleCount\DB\cycleCount.mdb"
    10.     newConnection.Open
    11.        
    12.     sql = "INSERT INTO MAIN ( PICKSL, SLOT, RES, PROD, [DESC], PACK, " & _
    13.           "[MANU ID], HITIX, [PALLET ID], [PALLET QTY], DFP ) " & _
    14.           "SELECT t.PICKSL, t.SLOT, t.RES, t.PROD, t.DESC, t.PACK, " & _
    15.           "t.[MANU ID], t.HITIX, t.[PALLET ID], t.[PALLET QTY], t.DFP " & _
    16.           "FROM Test AS t " & _
    17.           "WHERE Not Exists ( " & _
    18.             "SELECT * " & _
    19.             "FROM [ALTER] a " & _
    20.             "WHERE  a.PROD = t.PROD);"
    21.  
    22.     newConnection.Execute (sql)
    23.    
    24.     sql = "INSERT INTO MAIN ( PICKSL, SLOT, PROD, [DESC], PACK, " & _
    25.           "[MANU ID], HITIX, [PALLET QTY] ) " & _
    26.           "SELECT p.PICKSL, p.PICKSL, p.PROD, p.DESC, p.PACK, " & _
    27.           "p.MANUID, p.HITI, p.OH " & _
    28.           "FROM PICKSL AS p " & _
    29.           "WHERE Not Exists ( " & _
    30.             "SELECT * " & _
    31.             "FROM [ALTER] a " & _
    32.             "WHERE  a.PROD = p.PROD);"
    33.  
    34.     newConnection.Execute (sql)
    35.    
    36.     newConnection.Close
    37.     Set newConnection = Nothing
    38. End Sub
    39. '*ENDOF*INSERT()***************************************************************

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Question

    The syntax is right, assuming all field and table names are correct.
    What errors, if any, are you getting? Do both queries fail?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Question

    I'm not getting any errors; however, it does not insert the data from the TEST and ALTER tables into the MAIN table. And my program runs off the MAIN table. Any suggestions would be greatly appreciated. Thank you.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Question

    CORRECTION: I'm sorry it does not insert the data from the TEST and PICKSL tables into the MAIN table.

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Question

    I would need a copy of the database to help resolve the issue.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Question

    I can email it to you if you would like

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL Question

    Quote Originally Posted by manofsteel00
    I can email it to you if you would like
    Just zip it and attach it.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Question

    I had to scale down the database to attach it.
    Attached Files Attached Files

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

  9. #9
    Lively Member
    Join Date
    Apr 2005
    Posts
    91

    Re: SQL Question

    Hi,
    I Just inserted this into a form and it Updates Ok.

    VB Code:
    1. Private Sub CmdUpdate_Click()
    2. Dim Conn As ADODB.Connection
    3. Set Conn = New ADODB.Connection
    4. Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    5.         "Data Source=c:\vbcodedownload\cycle\cyclecount.mdb"
    6. Conn.Open
    7. Dim MyCommand As ADODB.Command
    8. Set MyCommand = New ADODB.Command
    9. MyCommand.ActiveConnection = Conn
    10. MyCommand.CommandText = "INSERT INTO MAIN ( PICKSL, SLOT, RES, PROD, [DESC], PACK, [MANU ID], HITIX, [PALLET ID], [PALLET QTY], DFP )" _
    11. & " SELECT t.PICKSL, t.SLOT, t.RES, t.PROD, t.DESC, t.PACK, t.[MANU ID], t.HITIX, t.[PALLET ID], t.[PALLET QTY], t.DFP" _
    12. & " FROM Test AS t WHERE Not Exists (SELECT *  FROM [ALTER] a WHERE  a.PROD = t.PROD)"
    13. Debug.Print MyCommand.CommandText
    14. MyCommand.Execute
    15. End Sub


  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Question

    ok I'll check. Thank you.

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: SQL Question

    I'm not getting any errors; however, it does not insert the data from the TEST and PICKSL tables into the MAIN table.
    I downloaded your database and using your code was able to insert the records into main with no problems. Sorry, no help at all and I am out of ideas.

    Is there anything else accessing the database while this code is executing? Other connections? Is it possible to upload your entire program?

  12. #12
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: SQL Question

    I've noticed that Access saved queries are treated differently than ones run from code. In my case, it's usually when checking for Null, but could be with your "Not Exists" type statement.
    If I write a query in SQL design view with some correct SQL syntax & save it, it will run correctly. The same syntax in code may give an error. Going back to the saved query and checking SQL view, Access has changed the syntax for checking Nulls, in my case. Not at work, so can't give you the exact problem, but it's something to do with IsNull(Foo) vs. Not Null vs IsNull(Foo) = False, etc.
    Tengo mas preguntas que contestas

  13. #13
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: SQL Question

    As fishy has tried, what happens if you only try to insert one record at a time?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    Re: SQL Question

    Thanks for everyone's help; Fishy's code worked. Thanks again.

    Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width