[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB6 - Refresh MSHflexgrid1 after updating it

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

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 50

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 50

Expert Comment

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

864 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