Results 1 to 9 of 9

Thread: Can't figure out why this record won't update

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    Can't figure out why this record won't update

    I have this function that updates a record. I can't get it to update 1 certain field, "SPR". It works fine when, a) after I close my application and re-open it, or b) when I substitute a hard value for the variable (aka, instead of SPRTemp, I use .5). Can anyone please take a look at this function and see what might be going wrong?

    vb Code:
    1. Private Sub txtSPRDiscount_Change()
    2. Dim SPRTemp As Single ' for temporary storage of SPRDiscount
    3.  
    4.      lstProgram.Enabled = False
    5.     If txtSPRDiscount <> "" Then SPRTemp = rPlace(txtSPRDiscount)
    6.    
    7.     If Not IsNull(SPRTemp) Then
    8.         If IsNumeric(SPRTemp) Then
    9.             If SPRTemp > 0 And SPRTemp < 100 Then
    10.                 SPRChange = True
    11.                 SPR2 = Val(Replace(txtSPRDiscount, "%", "")) / 100
    12.                
    13.                 If Not performComputeData Then ' Compute Data based from the SPR input
    14.                     Debug.Print "Error Computing Data"
    15.                
    16.                 Else ' if there's no error lstProgram will be enabled and function performComputeData will be initiated
    17.                    
    18.                     SPRTemp = SPRTemp / 100
    19.                    
    20.                     performComputeData
    21.                  '---- Update out_03_DealConfigs  based from SPR changes ----
    22.                    
    23.                     performMoveData
    24.                    
    25.                     editSPRTruckNotes txtNumber, ModelID, currConfigID, _
    26.                     "SPR=" & SPRTemp & _
    27.                     ", DealerNet=" & FinalDealerNet & _
    28.                     ", FinalCost=" & FinalCost & _
    29.                     ", FinalCurrentCost=" & FinalCurrentCost & _
    30.                     ", FinalGoingToCost=" & FinalGoingToCost & _
    31.                     ", Margin=" & FinalMargin & _
    32.                     ", CurrentMargin=" & FinalCurrentMargin & _
    33.                     ", GoingToMargin=" & FinalGoingToMargin & _
    34.                     ", MarginPercent=" & AdjustMarginPercent & _
    35.                     ", CurrentMarginPercent=" & AdjustCurrentMarginPercent & _
    36.                     ", GoingToMarginPercent=" & AdjustGoingToMarginPercent
    37.  
    38.                  SPR2 = SPRTemp
    39.                  lstProgram.Enabled = True
    40.                  End If
    41.                  
    42.             Else
    43.                 txtSPR = 0
    44.                
    45.             End If
    46.         End If
    47.     End If
    48.     MsgBox Val(SPRTemp)
    49. End Sub
    50.  
    51. Function editSPRTruckNotes(ByVal DealNumber As String, ByVal ModelID As Long, _
    52.                     ByVal ConfigId As Long, ByVal editString As String)
    53.    
    54.  '---- Function that will edit SPR in out_03_DealConfigs -----
    55.     Dim rs As Recordset
    56.     Dim dbObj As New dbFunctions
    57.     Dim i As Integer
    58.     dbObj.dbProp = strConn
    59.     mSQL = "SELECT * FROM out_03_DealConfigs WHERE DealNo = '" & DealNumber & "' AND " & _
    60.            " ModelID = " & ModelID & " AND ConfigurationID = " & ConfigId
    61.     Set rs = dbObj.getRS(mSQL, isException, errDesc)
    62.     If isException = 0 Then
    63.         If Not rs.EOF Then
    64.             If Not dbObj.editRecord("out_03_DealConfigs", editString, "DealNo = '" & DealNumber & "' AND " & _
    65.            " ModelID = " & ModelID & " AND ConfigurationID = " & ConfigId, errDesc) Then
    66.                 Debug.Print "Error Editing SPR"
    67.             End If
    68.         End If
    69.     End If
    70.     Set rs = Nothing
    71.     Set dbObj = Nothing
    72. End Function

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

    Re: Can't figure out why this record won't update

    I assume the performComputeData is where your UPDATE query is being run, but I don't see the code for that function.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    Re: Can't figure out why this record won't update

    Actually, performcomputeData simply updates some calculations. The update part is in the editSPRTruckNotes function.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can't figure out why this record won't update

    It does appear you are using some DLL or OCX, whatever allows you to create the dbFunctions object. What is that?

    Assuming we don't have access to that class (not sure what it is). Does it have a commit property/function or something similar that allows you to update the recordset after the change is made?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    Re: Can't figure out why this record won't update

    Good question. To be honest, I'm tweaking someone else's class module, dbFunctions, so I'm not exactly sure. I'm kinda a newbie when it comes to this. Here's the module I'm using. If this gets to complicated for a forum, I understand!

    (abbreviated):
    vb Code:
    1. Option Explicit
    2.  
    3. Dim udlConn As String
    4.  
    5. Private Function setConn(ByRef errConn As Boolean, ByRef errDesc As String, ByRef errNumber As Long) As ADODB.Connection
    6.     On Error GoTo errSetConn
    7.     Dim cn As New ADODB.Connection
    8.     Dim ConnString As String
    9.     Dim pass As String
    10.     pass = getDBPassword
    11.    
    12.     '----- Initialize the Connection String and retrieve the Password from dbAccess.ini -----
    13.     ConnString = "Provider=MSDataShape;" & _
    14.                  "Data Provider=Microsoft.Jet.OLEDB.4.0;" & _
    15.                  "Data Source=" & udlConn & ";Jet OLEDB:Database Password=" & pass
    16.  
    17.     If errConn = False Then
    18.         cn.CursorLocation = adUseClient
    19.         cn.ConnectionString = ConnString
    20.         cn.Open
    21.         Set setConn = cn
    22.     Else
    23.         Err.Description = errDesc
    24.         GoTo errSetConn
    25.     End If
    26.     Set cn = Nothing
    27.     Exit Function
    28. errSetConn:
    29.     Set cn = Nothing
    30.     errDesc = Err.Description
    31.     errNumber = Err
    32.     errConn = True
    33. End Function
    34.  
    35. Public Function getRS(ByVal strSQL As String, _
    36.                       ByRef isException As Long, ByRef errDesc As String) As ADODB.Recordset
    37.                      
    38.   '--- Initialize the recordset---
    39.     On Error GoTo errGetRS
    40.     Dim rs As New ADODB.Recordset
    41.     Dim errConn As Boolean
    42.     Dim errNumber As Long
    43.     rs.CursorLocation = adUseClient
    44.     rs.CursorType = adOpenDynamic
    45.     rs.ActiveConnection = setConn(errConn, errDesc, errNumber)
    46.     If errConn = True Then
    47.         Err.Description = errDesc
    48.  
    49.         GoTo errGetRS
    50.     Else
    51.         rs.Open strSQL
    52.         Set getRS = rs
    53.     End If
    54.     Set rs = Nothing
    55.     Exit Function
    56. errGetRS:
    57.     errDesc = Err.Description
    58.     isException = errNumber
    59. End Function
    60.  
    61. Public Function addRecord(ByVal tableName As String, _
    62.                           ByVal fieldList As String, _
    63.                           ByVal valueList As String, _
    64.                           ByRef errDesc As String) As Boolean
    65.                          
    66.  '--- Function adding Records in the Database ----
    67.     On Error GoTo errAddRecord
    68.     Dim isException As Long
    69.     Dim strSQL As String
    70.     strSQL = "INSERT INTO " & Trim(tableName) & "(" & Trim(fieldList) & ") VALUES(" & Trim(valueList) & ")"
    71.     getRS strSQL, isException, errDesc
    72.     If isException = 0 Then
    73.         addRecord = True
    74.     Else
    75.         Err.Description = errDesc
    76.         GoTo errAddRecord
    77.     End If
    78.     Exit Function
    79. errAddRecord:
    80.     errDesc = Err.Description
    81.     addRecord = False
    82. End Function
    83.  
    84. Public Function editRecord(ByVal tableName As String, _
    85.                           ByVal setList As String, _
    86.                           ByVal cond As String, ByRef errDesc As String) As Boolean
    87.     On Error GoTo errEditRecord
    88.     Dim isException As Long
    89.     Dim strSQL As String
    90.    
    91.   '---- Function to Edit Records ----
    92.     strSQL = "UPDATE " & Trim(tableName) & " SET " & Trim(setList) & " WHERE " & Trim(cond)
    93.     getRS strSQL, isException, errDesc
    94.    
    95.     Debug.Print strSQL
    96.     If isException = 0 Then
    97.         editRecord = True
    98.     Else
    99.          Debug.Print errDesc
    100.         Err.Description = errDesc
    101.         GoTo errEditRecord
    102.     End If
    103.     Exit Function
    104. errEditRecord:
    105.     errDesc = Err.Description
    106.     editRecord = False
    107. End Function
    108.  
    109. Public Function deleteRecord(ByRef errDesc As String, ByVal tableName As String, _
    110.                           Optional ByVal cond) As Boolean
    111.     On Error GoTo errDelRecord
    112.     Dim isException As Long
    113.     Dim strSQL As String
    114.    
    115.    '----- Function to Delete a set/ certain Recordset-------
    116.    
    117.     If IsMissing(cond) Then
    118.         strSQL = "DELETE FROM " & Trim(tableName)
    119.     Else
    120.         strSQL = "DELETE FROM " & Trim(tableName) & " WHERE " & Trim(cond)
    121.     End If
    122.     getRS strSQL, isException, errDesc
    123.     If isException = 0 Then
    124.         deleteRecord = True
    125.     Else
    126.         Err.Description = errDesc
    127.         GoTo errDelRecord
    128.     End If
    129.     Exit Function
    130. errDelRecord:
    131.     errDesc = Err.Description
    132.     deleteRecord = False
    133. End Function
    134.  
    135. Public Property Let dbProp(ByVal dbCn As String)
    136.     If dbCn <> "" Then
    137.         udlConn = dbCn
    138.     Else
    139.         udlConn = ""
    140.     End If
    141. End Property

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can't figure out why this record won't update

    I'm not the db guru on this site.
    But I do question one thing that a db guru can answer.
    Your EditRecord and DeleteRecord functions are calling getRS which does the "updating". In the getRS function, rs.Open strSQL is called. I always thought that rs.Execute should be called to modify the database, not rs.Open... rs.Open used to return a query whereas rs.Execute used to return/update a recordset.

    Unless someone with more knowledge offers a solution, you might want to change rs.Open to rs.Execute and see how it affects your project.
    edited: rs doesn't have an .Execute method. The connection does, so...
    change rs.Open strSQL to Set rs = setConn(errConn, errDesc, errNumber).Execute(strSQL)
    If the change works, other code in getRS should be modified. Here is what it might look like after tweaking:
    Code:
    Public Function getRS(ByVal strSQL As String, _
                          ByRef isException As Long, ByRef errDesc As String) As ADODB.Recordset
                          
      '--- Initialize the recordset---
        On Error GoTo errGetRS
        Dim rs As ADODB.Recordset
        Dim conn As ADODB.Connection
        Dim errConn As Boolean
        Dim errNumber As Long
        Set conn = setConn(errConn, errDesc, errNumber)
        If errConn = True Then
            Err.Description = errDesc
    
            GoTo errGetRS
        Else
            Set rs=conn.Execute(strSQL)
            Set getRS = rs
        End If
        Set rs = Nothing
        Exit Function
    errGetRS:
        errDesc = Err.Description
        isException = errNumber
    End Function
    Quote Originally Posted by MSDN
    ... It is not a good idea to use the Source argument of the Open method to perform an action query that does not return records because there is no easy way to determine whether the call succeeded. The Recordset returned by such a query will be closed.
    Quote source


    P.S. see the edited note above.
    Also, IMO the class is far from efficient. Every time you add, edit or delete a record a new connection is opened to the database. This is a performance hit since establishing the connection takes time. Adding/deleteing/editing records in a loop would be much faster if the connection was not created in each call. Also, many will tell you that explcitly setting ADODB objects to Nothing is preferred when the object is no longer needed. Every time you add/edit/delete a record, the connection and recordsets are not explicitly destroyed. The class can be modified easily to make it more efficient, by simply creating/storing the connection when dbProp is set and then destroying/releasing the connection during the class' Terminate event. Of course the indivdual routines would need to be tweaked so that they do not call setConn, but use/re-use the stored connection. And additional checks should be then applied: For instance a connection may terminate for various reasons: timed-out, server error, etc. Knowing this, the getRS function would validate the connection is still alive and reestablish if needed.

    Realize this is not your project as you stated. You might want to inform the owner of comments made.
    Last edited by LaVolpe; Jun 23rd, 2009 at 02:25 PM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can't figure out why this record won't update

    Correct... .Open should be used to OPEN the data.... once you have the data you can manuipulate it, then send the changes back to the database. This can be done using the .UpdateBatch of the recordset, or by looping and using .Execute to call "action queries" ... BUT, if I remember right, the execute is on the COMMAND object, not the recordset itself.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can't figure out why this record won't update

    Quote Originally Posted by techgnome View Post
    ... If I remember right, the execute is on the COMMAND object, not the recordset itself.-tg
    Yepper, caught that too. I modified my statement before you posted
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2009
    Posts
    5

    Re: Can't figure out why this record won't update

    Guys, you totally pointed it out. I knew there was something along those lines that I was missing. .Execute makes so much more sense than .open... I knew something wasn't right there, but I had no clue what. Thanks for revising that code... so much better than what I had. This forum rocks!

Tags for this Thread

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