Results 1 to 17 of 17

Thread: Database Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Database Help

    Hi, i'm new here but i'm having alot of trouble using the SQL functions to update stuff in my database.... the code is below can anyone here help out and tell me whats wrong with it please......

    VB Code:
    1. Dim MyConn As ADODB.Connection
    2.     Dim MyRecSet As ADODB.Recordset
    3.    
    4.    
    5.     Set MyConn = New ADODB.Connection
    6.     MyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=MS Access Database;Initial Catalog=D:\Documents and Settings\christopher.lynch\Desktop\Manual Time And attendance\Manualrecords.mdb"
    7.    
    8.     MyConn.Open
    9.     MyConn.Execute ("UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text & "' WHERE StudentName = '" & CmbStudent.Text & "'")
    10.        
    11.    
    12.     MyConn.Close

    What is ment to happen is, I have thre combe boxes with selection. I what to select a name to alter, class for that name and whether the student is absent or present.....

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Question Re: Database Help

    The error i get is too few parameters on the linew containing the MyConn.Execute ("UPDATE.....

    Please halp i've been at this part of my program for almst 2 days now!!!!!!

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    Welcome to the forum.

    Although I believe you can use the .EXECUTE method of the connection, we do not do it that way...

    We create a command object...

    something like this:

    Code:
    Dim objCmd as ADODB.Command
    
    Set objCmd = New ADODB.Command
    
    objCmd.ActiveConnection = myConn
    objCmd.CommandText = "UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text _
        & "' WHERE StudentName = '" & CmbStudent.Text & "'"
    
    objCmd.CommandType = adCmdText
    objCmd.Execute
    I typed that free hand - so it could have syntax errors...

    HTH.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    Ya i found that in a tutorial and this is the code i have now
    VB Code:
    1. Dim MyConn As ADODB.Connection
    2.     Dim MyRecSet As ADODB.Recordset
    3.     Dim strSQL As String
    4.    
    5.    
    6.     Set MyConn = New ADODB.Connection
    7.     MyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=MS Access Database;Initial Catalog=D:\Documents and Settings\christopher.lynch\Desktop\Manual Time And attendance\Manualrecords.mdb"
    8.     MyConn.Open
    9.     sSQL = ("UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text & "' WHERE StudentName = '" & CmbStudent.Text & "'")
    10.    
    11.    
    12.    
    13.     Debug.Print strSQL
    14.    
    15.     MyConn.Execute strSQL
    16.     MyConn.Close

    do i take out the Top two lines or replace the Myconn and MySetRec with the sSQL?

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    sSQL is a typo - you mean strSql - right?

    You have no need for the recordset object - as the UPDATE statement can only be executed - it does not return a recordset.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    Yes strSQL and take out the recordset got ya i'll be back in a mo with errors if any

    Thanks

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    This is the error screen that appears

    Run Time Error 3001
    Arguments are of the wrong type, are out of acceptable range or are in conflict with one another....

    this is the code i now have and the error occurs on the line
    objCmd.ActiveConnection = MyConn

    VB Code:
    1. Dim objCmd As ADODB.Command
    2.  
    3.     Set objCmd = New ADODB.Command
    4.  
    5.     objCmd.ActiveConnection = MyConn
    6.     objCmd.CommandText = "UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text & "' WHERE StudentName = '" & CmbStudent.Text & "'"
    7.  
    8.     objCmd.CommandType = adCmdText
    9.     objCmd.Execute

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    Do you still have this code - because it's needed to open the connection...

    VB Code:
    1. Dim MyConn As ADODB.Connection
    2.     Set MyConn = New ADODB.Connection
    3.     MyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=MS Access Database;Initial Catalog=D:\Documents and Settings\christopher.lynch\Desktop\Manual Time And attendance\Manualrecords.mdb"
    4.     MyConn.Open

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    Sorry about that just out it back in and now it says too few parameters for the objCmd.Execute line

  10. #10
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    Please post the results of the

    DEBUG.PRINT strSQL

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    This might be a stupid question but i've taken out strSQL so will i put in debug.print objCmd.CommandText

    This is the code i have now

    VB Code:
    1. Dim MyConn As ADODB.Connection
    2.     Dim objCmd As ADODB.Command
    3.    
    4.     Set MyConn = New ADODB.Connection
    5.     MyConn.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=MS Access Database;Initial Catalog=D:\Documents and Settings\christopher.lynch\Desktop\Manual Time And attendance\Manualrecords.mdb"
    6.     MyConn.Open
    7.  
    8.     Set objCmd = New ADODB.Command
    9.  
    10.     objCmd.ActiveConnection = MyConn
    11.     objCmd.CommandText = "UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text & "' WHERE StudentName = '" & CmbStudent.Text & "'"
    12.  
    13.     objCmd.CommandType = adCmdText
    14.     objCmd.Execute
    15.    
    16.     MyConn.Close

  12. #12
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    I like the strSQL technique - that way you can build the string and look at it more easily...

    VB Code:
    1. strSQL = "UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text & "' WHERE StudentName = '" & CmbStudent.Text & "'"
    2. Debug.Print strSQL
    3. objCmd.CommandText = strSQL

    We don't do ACCESS here (we are a MS SQL Server shop) - but don't you need a semi-colon at the end of an access query?

    Shouldn't the actual statement be:

    VB Code:
    1. strSQL = "UPDATE Manual SET '" & CmbClass.Text & "' = '" & CmbAttendance.Text & "' WHERE StudentName = '" & CmbStudent.Text & "';"

    But at any rate, do the DEBUG.PRINT and paste the IMMEDIATE WINDOW contents here on the forum - thanks...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    UPDATE Manual SET Physics = 'Absent' WHERE StudentName = 'Chris Lynch'

    This is what i get, which looks right but its not doin that..... From the line above it should right absent the field called physics for the name chris lynch

  14. #14
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    You actually have a column called PHYSICS in the TABLE called MANUAL?

    Does that query statement execute in whatever kind of QUERY BUILDER that ACCESS has for you to test these things (remember I don't use ACCESS)...

    And once again - doesn't ACCESS require a semi-colon at the end of a QUERY?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    133

    Re: Database Help

    yes i have column called physics and a table called manual

    I don't know what you mean by this as its my first time ever using access or databases even at that "Does that query statement execute in whatever kind of QUERY BUILDER that ACCESS has for you to test these things (remember I don't use ACCESS)..."

    And in another forum i was using to find help they said nothing about a semi colon

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Database Help

    In your code you have:
    ...SET '" & CmbClass.Text & "' = '" ...

    Shouldnt it be this instead?:
    ...SET [" & CmbClass.Text & "] = '" ...

    (or this: ...SET " & CmbClass.Text & " = '" ... )

  17. #17
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Help

    Quote Originally Posted by chris lynch
    UPDATE Manual SET Physics = 'Absent' WHERE StudentName = 'Chris Lynch'

    This is what i get, which looks right but its not doin that..... From the line above it should right absent the field called physics for the name chris lynch
    SI is right - you should not have the ' quotes around the column name...

    But then again if you copy/pasted the immediate window into this post - you didn't at that time...

    But it appears from the code in your last post that you do...

    I'm confused by what you have posted.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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