Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB6 - Refresh MSHflexgrid1 after updating it

Posted on 2014-02-26
5
Medium Priority
?
433 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 49

Accepted Solution

by:
Martin Liss earned 2000 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 49

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 49

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

636 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