Results 1 to 5 of 5
  1. #1
    reysy28 is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    7

    Update statement

    Hi Guys, what's wrong with these commands......

    Private Sub Command49_Click()
    Dim cn As ADODB.Connection
    Dim m_strSql, m_elapsed As String

    Me.EndTime.Value = Format(Time, "HH:MM:SS")
    Set cn = CurrentProject.Connection

    m_elapsed = Format(TimeValue(EndTime) - TimeValue(BegTime), "HH:MM:SS")
    m_strSql = "update AppDataFile set Lang_Time = m_elapsed.value where AppId =" + CStr(Form_Examination.AppID.Value) + ";"
    cn.Execute m_strSql


    Set cn = Nothing



    DoCmd.OpenForm "Tech_Examination"


    End Sub

    Am having an error at "cn.Execute m_strSql" NO VALUE GIVEN FOR ONE OR MORE REQUIRED PARAMETER

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Since m_elapsed is a variable, it needs to be outside the quotes when building m_strSQL, i.e.
    Code:
    m_strSql = "update AppDataFile set Lang_Time = '" & m_elapsed.value & "' where AppId =" & CStr(Form_Examination.AppID.Value) & ";"
    I find the best way to create these type of SQL statements is to manually create an example of a query that does what I want. Then switch to SQL view and copy/paste the code that results. This is pretty much exactly what the code you are building needs to look like.

    Then, in your VBA code, temporarily comment out the line that runs your SQL code (the "Execute" line), and replace it with a MsgBox that returns the SQL code you just built, i.e.
    Code:
    MsgBox m_strSql
    Then compare it to the code you copied out earlier. Does it look similar? Does it have the correct values, quotes, etc in all the right places?
    Once you are satisfied that it does, you can uncomment the VBA code that runs the SQL code, and get rid of the MsgBox.

  3. #3
    reysy28 is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    7
    Thanks JoeM, am having error in m_elapsed variable.... what I did is removed the .value and it works!!!!

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    I realize your code is working, but for reference:

    This does Not do what you think
    Dim m_strSql, m_elapsed As String

    m_strSql will be a variant
    m_elapsed will be a string

    You have to explicitly Dim each variable, otherwise it will be a variant by default.

    You could use

    Dim m_strSql As String, m_elapsed As String
    or
    Dim m_strSql As String
    Dim m_elapsed As String

  5. #5
    reysy28 is offline Novice
    Windows 8 Access 2013
    Join Date
    Oct 2014
    Posts
    7
    Noted Sir Orange.. thanks.....

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Help on SQL Statement for Update Query
    By KCC47 in forum Queries
    Replies: 4
    Last Post: 12-31-2013, 07:02 AM
  2. If statement in an update query
    By tsvetkovdimitar in forum Queries
    Replies: 35
    Last Post: 11-25-2013, 07:57 PM
  3. UPDATE TO IIF Statement
    By anilytics in forum Queries
    Replies: 5
    Last Post: 03-09-2012, 03:45 AM
  4. SQL Update statement help
    By kalltim in forum Access
    Replies: 6
    Last Post: 01-18-2012, 07:30 AM
  5. Replies: 2
    Last Post: 07-20-2011, 02:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums