Solved

VB6 - Refresh MSHflexgrid1 after updating it

Posted on 2014-02-26
5
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 47

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 47

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 47

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

738 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