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
Solved

VB6 - Refresh MSHflexgrid1 after updating it

Posted on 2014-02-26
5
409 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 46

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 46

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 46

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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 …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

809 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