Line acting abnormally in For each loop in VBA code in MSAccess

Ok, so I started with a question and made a lot of code changes based on suggestions from this site and others, so I figured I should create a new question.

Even though my code is shorter and more efficient, the same problem persists; I am using a strSQL string that contains and the INSERT statement I want to execute. I have a FOR EACH loop that goes through every control on my MSAcess form (makes sure they are a textbox, dropdown or checkbox) and determines if the field has been modified. If it has, it generates a query string to log the change - and stores it in strSQL.

The problem is that the same request is being executed over and over again. I've added a DEBUG.PRINT statement before and after the line that executes the query string and the debugger shows the line is CHANGED! Yes, you read that correctly, it seems impossible, but I took screenshots.

First, my code:

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim C As Control
    For Each C In Controls
        Select Case C.ControlType
            Case acTextBox, acComboBox, acCheckBox
                Dim strOriginalValue, strCurrentValue, strSQL As String

                strOriginalValue = IIf(IsNull(C.OldValue), "", IIf(C.OldValue = vbTrue Or C.OldValue = vbFalse, IIf(C.OldValue = vbTrue, "Yes", "No"), C.OldValue))
                strCurrentValue = IIf(IsNull(C.Value), "", IIf(C.Value = vbTrue Or C.Value = vbFalse, IIf(C.Value = vbTrue, "Yes", "No"), C.Value))

                If strOriginalValue <> strCurrentValue Then
                    strSQL = "INSERT INTO fringefestival_changes (change_time,change_admin,action_taken,user_affected,year_affected,field_affected,type_affected,old_value,new_value) VALUES (NOW(),'" & ThisUserName() & "','Edit'," & [id] & ",0,'" & C.ControlSource & "','Administrator','" & Replace(strOriginalValue, "'", "") & "','" & Replace(strCurrentValue, "'", "") & "')"
                    Debug.Print "Before: " & strSQL
                    CurrentDb.Execute strSQL, dbFailOnError
                    Debug.Print "After: " & strSQL
                End If
        End Select
    Next
End Sub

      

Here are the debugger results:

Before: INSERT INTO fringefestival_changes (change_time,change_admin,action_taken,user_affected,year_affected,field_affected,type_affected,old_value,new_value) VALUES (NOW(),'ajohnson','Edit',3,0,'indoor_performers_tab','Administrator','No','Yes')
After: INSERT INTO fringefestival_changes (change_time,change_admin,action_taken,user_affected,year_affected,field_affected,type_affected,old_value,new_value) VALUES (NOW(),'ajohnson','Edit',3,0,'indoor_performers_tab','Administrator','No','Yes')
Before: INSERT INTO fringefestival_changes (change_time,change_admin,action_taken,user_affected,year_affected,field_affected,type_affected,old_value,new_value) VALUES (NOW(),'ajohnson','Edit',3,0,'volunteers_tab','Administrator','No','Yes')
After: INSERT INTO fringefestival_changes (change_time,change_admin,action_taken,user_affected,year_affected,field_affected,type_affected,old_value,new_value) VALUES (NOW(),'ajohnson','Edit',3,0,'volunteers_tab','Administrator','No','Yes')

      

Secondly, my screenshots ... firstly the debugger: 1 and 2 and secondly the results: here - note that the debugger is also so I took two screenshots and the results have a line colored in as it has nothing to do with this problem.

Note that the only column that should be different in the above example should be "field_affected".

I'm at a complete loss, I have no idea why it prints the correct line to the debugger and does something else.

EDIT: I used DoCmd.RunSQL before using CurrentDb.Execute, but that required me to disable and re-enable warnings. Since the result was the same for both (same error), I used a one line solution instead of a three line solution.

UPDATE: Shoutout for shahkalpesh to help me figure out that the data is indeed inserted correctly, and I can see it in SQL Server Management Studio, but MS Access still displays incorrectly ... the question is why?

FINAL EDIT: Fixed by adding an identity / auto-increment integer column to the table, I suspect the lack of different field values ​​confused MSAccess (though it really has no reason) - the morale here is M $ - silly

-2


source to share


7 replies


Does this form match the data source? This is the most common way to use Access, and if you had a hard time verifying it with SQL.



Form_BeforeUpdate () is typically used when a form is bound to a data source. Typically, you would not use this event to determine if the values ​​of the controls have changed and if you need to write back to the database ...

+1


source


If you can go to a specific SQL Server table (using Query Analyzer or Enterprise Mgr) and see if it works as you expect. I doubt the linked table in VBA might be showing you the wrong picture.



Edit: If you have SQL Profiler, look at what is running there to confirm your doubt that the same query will be executed.

+1


source


You are running these queries against currentdb (which I assume is access-db) but are actually using the SQL server database where do you want the changes to happen?

Are these tables in sql server linked to tables in access-db or which method are you using?

0


source


Have you checked the table for broken triggers? This is a long shot, but some very strange issues have been caused by failed / broken triggers before and are hard to understand before you notice the trigger ...

0


source


Fixed by adding an auto-increment primary key column to the change table.

Foolish access ...

0


source


Oh, you're using SQL Server too. I have had very good luck using triggers to audit data changes for Access. There are a few quirks, but linking tables over ODBC using strong authentication makes it very easy.

OK, so you can't or don't want to use triggers. When SQL interface is based on SQL completion, I usually use ADO objects in my VBA code for this kind of thing. Linked tables with ODBC are great for linked forms, but nothing compares ADO for going directly to SQL Server.

Looking for more information on linking tables using strong authentication or help using ADO with SQL Server?

-1


source


I have to say that the discussion around this issue is driving me crazy. With linked ODBC tables, you start with linked forms. There is simply no reason to use pooled joins. If you are hostile to the Access implementation, how to do it, stop using ACCESS.

-1


source







All Articles