Solved

VB6 - Refresh MSHflexgrid1 after updating it

Posted on 2014-02-26
5
396 Views
Last Modified: 2014-02-27
Hi

I have a problem when i update my MSHFlexgrid1 to refresh the data inside the grid so that i can see the update i just did with the below code:
Dim a As Long

a = MsgBox("Are you sure you want to update requester " & EXISTING_USER.Text & " ?", vbYesNo)

If (a = 6) Then
        Dim MyConn As ADODB.Connection
    Dim MyRecSet1 As New ADODB.Recordset

    Set MyConn = New ADODB.Connection
    MyConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                   "Data Source=" & MDIForm1.dta_base_link_audit_tms.Text
    MyRecSet1.Open "SELECT * FROM [spot_rate_requester] WHERE User='" & EXISTING_USER.Text & "'", MyConn, adOpenForwardOnly, adLockReadOnly

    If MyRecSet1.EOF = False Then
        If MyRecSet1.EOF = False Then
       Dim strSQL As String

strSQL = " Update [spot_rate_requester] "
strSQL = strSQL & " SET "
strSQL = strSQL & "Region= '" & Replace(region1.Text, "'", "''") & "'"
strSQL = strSQL & " , "
strSQL = strSQL & " Authorized ='" & Replace(yesno1.Text, "'", "''") & "'"
strSQL = strSQL & " Where "
strSQL = strSQL & " User ='" & Replace(EXISTING_USER.Text, "'", "''") & "'"


MyConn.Execute (strSQL)
End If
    MyRecSet1.Close
End If
End If


 MSHFlexGrid1.clear
    MSHFlexGrid1.Rows = 2
    MSHFlexGrid1.Cols = 2

EXISTING_USER = ""
region1 = ""
yesno1 = ""
MSHFlexGrid1.Refresh

Open in new window


The only time i can see the update is if i totally close the form and re open it.
On the form load, the below macro run:
   Dim sSQL2 As String
    Dim oRST2 As ADODB.Recordset
    Set oRST2 = New ADODB.Recordset
    Dim oConnect2 As ADODB.Connection
    Set oConnect2 = New ADODB.Connection

        oConnect2.CursorLocation = adUseClient
    oConnect2.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                   "Data Source=" & MDIForm1.dta_base_link_audit_tms.Text

    'Now to populate or fill the grid with your record do this :

    'oConnect2.cusorlocation = adUseClient
    oRST2.Open "SELECT * FROM spot_rate_requester", oConnect2, adOpenKeyset, adLockOptimistic
    Set MSHFlexGrid1.DataSource = oRST2
    oRST2.Requery


    oRST2.Close
    oConnect2.Close
    Set oRST2 = Nothing
    Set oConnect2 = Nothing

Open in new window


How can i refresh it automatically without having to close the form?

Thanks for your help
0
Comment
Question by:Wilder1626
  • 3
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 39891873
As posted your code was hard to follow so I reformat it so that I could better understand what was going on. This is the result of that reformatting.
Dim a As Long

a = MsgBox("Are you sure you want to update requester " & EXISTING_USER.Text & " ?", vbYesNo)

If (a = 6) Then
    Dim MyConn As ADODB.Connection
    Dim MyRecSet1 As New ADODB.Recordset
    
    Set MyConn = New ADODB.Connection
    MyConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=" & MDIForm1.dta_base_link_audit_tms.Text
    MyRecSet1.Open "SELECT * FROM [spot_rate_requester] WHERE User='" & EXISTING_USER.Text & "'", MyConn, adOpenForwardOnly, adLockReadOnly

    If MyRecSet1.EOF = False Then
        If MyRecSet1.EOF = False Then
            Dim strSQL As String

            strSQL = " Update [spot_rate_requester] "
            strSQL = strSQL & " SET "
            strSQL = strSQL & "Region= '" & Replace(region1.Text, "'", "''") & "'"
            strSQL = strSQL & " , "
            strSQL = strSQL & " Authorized ='" & Replace(yesno1.Text, "'", "''") & "'"
            strSQL = strSQL & " Where "
            strSQL = strSQL & " User ='" & Replace(EXISTING_USER.Text, "'", "''") & "'"
            
            
            MyConn.Execute (strSQL)
        End If
        MyRecSet1.Close
    End If
End If


MSHFlexGrid1.Clear
MSHFlexGrid1.Rows = 2
MSHFlexGrid1.Cols = 2

EXISTING_USER = ""
region1 = ""
yesno1 = ""
MSHFlexGrid1.Refresh

Open in new window

The first thing I noticed is that you check twice for the same condition at lines 14 and 15 which is unnecessary, so if you should probably remove lines 15 and 28.

The second thing I noticed and what I believe is the source of your problem, is that while you query the database in that code, you don't do anything with the result.

In this version of the code I've removed the two lines I suggested above and added the code at lines 27 and 28. Let me know if that helps.

Dim a As Long

a = MsgBox("Are you sure you want to update requester " & EXISTING_USER.Text & " ?", vbYesNo)

If (a = 6) Then
    Dim MyConn As ADODB.Connection
    Dim MyRecSet1 As New ADODB.Recordset
    
    Set MyConn = New ADODB.Connection
    MyConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
               "Data Source=" & MDIForm1.dta_base_link_audit_tms.Text
    MyRecSet1.Open "SELECT * FROM [spot_rate_requester] WHERE User='" & EXISTING_USER.Text & "'", MyConn, adOpenForwardOnly, adLockReadOnly

    If MyRecSet1.EOF = False Then
        Dim strSQL As String

        strSQL = " Update [spot_rate_requester] "
        strSQL = strSQL & " SET "
        strSQL = strSQL & "Region= '" & Replace(region1.Text, "'", "''") & "'"
        strSQL = strSQL & " , "
        strSQL = strSQL & " Authorized ='" & Replace(yesno1.Text, "'", "''") & "'"
        strSQL = strSQL & " Where "
        strSQL = strSQL & " User ='" & Replace(EXISTING_USER.Text, "'", "''") & "'"
        
        
        MyConn.Execute (strSQL)
        Set MSHFlexGrid1.DataSource = MyRecSet1
        MyRecSet1.Requery
        MyRecSet1.Close
    End If
End If


MSHFlexGrid1.Clear
MSHFlexGrid1.Rows = 2
MSHFlexGrid1.Cols = 2

EXISTING_USER = ""
region1 = ""
yesno1 = ""
MSHFlexGrid1.Refresh

Open in new window

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 39892221
Hi MartinLiss

Thanks for making my code more clean.

I'm having a runtime error 30023 on : Set MSHFlexGrid1.DataSource = MyRecSet1
when the result from MSHFlexGrid1.DataSource = Nothing.

looks like this line is the only error i get cause when i remove it, the data base gets updated but the MSHFlexgrid1 stays empty.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39892350
Please describe the purpose of each of the two samples of code you originally posted.
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 39893611
I was able to make it work without: Set MSHFlexGrid1.DataSource = MyRecSet1, just be querying again the same query that feed the MSHflexgrid1 at the beginning.

Thanks again for your help
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 39893643
You're welcome.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now