Results 1 to 9 of 9

Thread: [RESOLVED] VB.NET Too Fast For MS Access ?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Rome, Italy
    Posts
    10

    Resolved [RESOLVED] VB.NET Too Fast For MS Access ?

    All,
    I have a sync problem between VB.NET and a MS Access database.

    I have an unbounded datagrid which shows the content of a table.

    When I want to delete a record from the Database, I select the row in the grid, which has also the ID of the record, and I perform a command like
    "Delete from mytable where ID = selected_id"

    After this sql command, I perform a a "SELECT * from mytable" statement, to show the changes in the datagrid.

    The problem is that between the Delete and the Select statements, the code runs so fast that the Select still gets the previous content of the table, so the record which has been just 'deleted', still appears in the datagrid.

    If I re-load after a second the datagrid, then it reflects the changes.

    Question: is there any way to know when the table has been effectively updated ?

    I have tryed also using transaction (begin and commit), but no luck. The problem arises when in the table there are at least 15-20 records or more. When the table contains less records, then it is 'fast enough', and this issue does not arise.

    Any help would be greately appreciated.

    Sergio.

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

    Re: VB.NET Too Fast For MS Access ?

    I don't know yet how but I in VB6.0 I would set the CursorLocation of the recordset to adUseClient...
    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

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: VB.NET Too Fast For MS Access ?

    Hmm.. for the time being, you can try just sleeping the thread..

    System.Threading.Thread.Sleep(1000) ' this would sleep for 1 second

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Rome, Italy
    Posts
    10

    Re: VB.NET Too Fast For MS Access ?

    Thanks, but OleDBConnection does not seem to have any CursorLocation property to set to "adUseClient" .

    I've tryed using it in a ADO connection, but I get the same problem, that is, the database is not yet updated when the next Select is performed. This is the scenario:

    - Delete record using ADO and Cursorlocation = adUseClient (the record is going to be deleted from Access DB)
    - Select * from mytable (re-load the table content in the datagrid)

    result: the datagrid content does not show the changes. If I re-load it again after some second, then I see the change in the datagrid.

    The sleep threading solution is indeed a workaround, but it gives the impression the application hangs, so I prefere not to use it.

    How would you get around this problem ?

    Sergio.

  5. #5
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: VB.NET Too Fast For MS Access ?

    I have run into this with Access in the past. The easiest way to get around it is to wrap your updates, deletes and inserts into a Transaction and then post them. That will force Access to process everything immediately.

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Rome, Italy
    Posts
    10

    Re: VB.NET Too Fast For MS Access ?

    @Aspnot,
    yes I did tryed using a transaction, (an OLEDB transaction) but it didn't work.
    Basically I've done something like:

    OLEDB begin transaction
    delete record
    OLEDB transaction commit
    re-load the datagrid content (with "Select * from mytable" statement)

    Sadly, same "sync" problem. I mean, also in this way, the datagrid does not show the updated table content. If i re-load it again, with another Select, then the datagrid shows the changes.

    A workaround would be to put a messagebox every time a record is deleted or added. But this is not accepted as a solution, as it is not really user-friendly for the final users.

    The way I'm looking for, should perform the datagrid update - using the Select statement - only when the table has been finally updated.

    Sergio.
    Last edited by oigres; Oct 20th, 2005 at 07:47 AM.

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

    Re: VB.NET Too Fast For MS Access ?

    dee-u - by design ADO.NET is disconnected client-side, there's no longer a cursor location option.

    oigres - Can you post your code on how you are populating your datatables and performing the deletes?

    -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

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Rome, Italy
    Posts
    10

    Re: VB.NET Too Fast For MS Access ?

    Quote Originally Posted by techgnome
    oigres - Can you post your code on how you are populating your datatables and performing the deletes?
    -tg
    This is how I delete the entry from Access DB (the commented statements are there why I have also tried with transaction begin and commit):

    VB Code:
    1. Public Function sql_cmd(ByVal sql As String) As Boolean
    2.  
    3.         'MsgBox(Application.ExecutablePath)
    4.  
    5.  
    6.  Dim oleconnstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath
    7.         Dim oleconnection As New OleDbConnection(oleconnstring)
    8.  
    9.         Dim olecommand As New OleDbCommand
    10.         'Dim oleTrans As OleDb.OleDbTransaction
    11.  
    12.         Try
    13.  
    14.             oleconnection.Open()
    15.  
    16.             'oleTrans = oleconnection.BeginTransaction()
    17.             olecommand.Connection = oleconnection
    18.             'olecommand.Transaction = oleTrans
    19.             olecommand.CommandText = sql
    20.             olecommand.ExecuteNonQuery()
    21.  
    22.  
    23.  
    24.             'oleTrans.Commit()
    25.  
    26.  
    27.             REM
    28.             'oleTrans = oleconnection.BeginTransaction
    29.  
    30.             'olecommand.Connection = oleconnection
    31.             'olecommand.Transaction = oleTrans
    32.  
    33.             'olecommand.CommandText = strSQL
    34.             'olecommand.ExecuteNonQuery()
    35.  
    36.             'olecommand.CommandText = "SELECT MAX(idMSG) FROM MessageLog"
    37.             'lngResult = olecommand.ExecuteScalar()
    38.  
    39.             'oleTrans.Commit()
    40.  
    41.             REM
    42.  
    43.  
    44.         Catch ex As Exception
    45.  
    46.             MsgBox(ex.Source & vbCr & ex.Message & vbCr & "pfad=" & dbpath & vbCr & "sql=" & sql, MsgBoxStyle.Exclamation)
    47.             Return False
    48.  
    49.         Finally
    50.  
    51.  
    52.             'oleconnection.Close()
    53.             'olecommand = Nothing
    54.             'oleconnection = Nothing
    55.             'Clear objects
    56.             If Not IsNothing(oleconnection) Then
    57.                 If oleconnection.State = ConnectionState.Open Then oleconnection.Close()
    58.                 oleconnection.Dispose()
    59.             End If
    60.  
    61.             olecommand.Dispose()
    62.  
    63.  
    64.         End Try
    65.  
    66.         WSS.table_changed = True
    67.         Return True
    68.  
    69.  
    70.     End Function
    ---------------------------------------

    I've also tried using ADO:

    VB Code:
    1. Public Function ADO_sql_cmd(ByVal sql As String) As Boolean
    2.  
    3.         Dim ADOconnstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath
    4.         Dim ADOconnection As New ADODB.Connection
    5.         Dim ADOcmd = New ADODB.Command
    6.  
    7.         Try
    8.  
    9.             ADOconnection.CursorLocation = ADODB.CursorLocationEnum.adUseClient
    10.             ADOconnection.Open(ADOconnstring)
    11.  
    12.             ADOcmd.activeconnection = ADOconnection
    13.             ADOcmd.commandtext = sql
    14.             ADOcmd.execute()
    15.             ADOcmd.activeconnection.close()
    16.  
    17.  
    18.  
    19.         Catch ex As Exception
    20.  
    21.             MsgBox(ex.Source & vbCr & ex.Message & vbCr & "pfad=" & dbpath & vbCr & "sql=" & sql, MsgBoxStyle.Exclamation)
    22.             Return False
    23.  
    24.         Finally
    25.  
    26.  
    27.             'oleconnection.Close()
    28.             'olecommand = Nothing
    29.             'oleconnection = Nothing
    30.             'Clear objects
    31.             If Not IsNothing(ADOconnection) Then
    32.                 If ADOconnection.State = ConnectionState.Open Then ADOconnection.Close()
    33.                 'ADOconnection.Dispose()
    34.             End If
    35.  
    36.             ADOcmd = Nothing
    37.  
    38.  
    39.         End Try
    40.  
    41.         WSS.table_changed = True
    42.         Return True
    43.  
    44.  
    45.     End Function

    ---------------------------------------
    This is how I populate the grid (it's a Janus grid):

    VB Code:
    1. Public Sub DBJanusGrid_Fill_Oledb(ByVal dbconnection As OleDbConnection, _
    2.  ByVal sql As String, ByRef grid As Janus.Windows.GridEX.GridEX)
    3.  
    4.  
    5.         Try
    6.  
    7.             Dim ds As New DataSet
    8.             Dim da As New OleDb.OleDbDataAdapter(sql.ToString, dbconnection)
    9.             Dim table As String = "table"
    10.             Dim cols As Integer
    11.             ds.Clear()
    12.             da.Fill(ds, table)
    13.  
    14.             'fill the grid with data
    15.             grid.DataSource = ds.Tables(table)
    16.             grid.RetrieveStructure()
    17.  
    18.             grid.Refresh()
    19.  
    20.             ds.Dispose()
    21.             da.Dispose()
    22.  
    23.         Catch ex As Exception
    24.             MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler: DBJanusGrid füllen Oledb")
    25.         End Try
    26.  
    27.     End Sub

    ---------------------------------------

    So, what I do in the code is:
    VB Code:
    1. sql = "delete from mytable where id = 2"
    2. 'ADO_sql_cmd(sql)
    3. sql_cmd(sql)
    4. 'then I re-load the grid
    5. 'MsgBox("Success !")
    6. sql = "select * from mytable"
    7. DBJanusGrid_Fill_Oledb(dbconnection, sql, datagrid)
    Sadly, when the select is executed, the grid still shows the old table content.
    Note that if I uncomment the msgbox, then the grid shows the right table content, because the time spent to click on the ok button seems that gives enough time to Access to update itself.

    Sergio.
    Last edited by oigres; Oct 20th, 2005 at 10:55 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 1999
    Location
    Rome, Italy
    Posts
    10

    Re: VB.NET Too Fast For MS Access ?

    Solved !

    The key is to reconnect to the database:
    Quote Originally Posted by Pirate
    Reconnect again to your database , I've done the same with no problems.
    Here is the thread:
    http://www.vbforums.com/showthread.p...=oledb+refresh

    Thank you Pirate

    Sergio.

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