Solved

How to refresh datagrid with code

Posted on 2000-02-20
5
164 Views
Last Modified: 2010-05-02
What I hope is to let user can re-query data from database by typing SQL command in the textbox and clicking command button.

The result is that it can work only in the first query. Thatis, datagrid will not changed when new SQL command is conduct.




Gerenal Declaration

Dim rec As ADODB.Recordset
Dim con As ADODB.Connection

Private Sub Command1_Click()


On Error Resume Next
rec.CursorLocation = adUseClient

a = Text1.Text
rec.Open a, con
rec.Requery

Set DataGrid1.DataSource = rec
DataGrid1.Refresh
End Sub

Private Sub Form_Load()
Set con = New ADODB.Connection
Set rec = New ADODB.Recordset

con.Open "DSN=pubs;UID=edwardchung;PWD=;"
End Sub
0
Comment
Question by:tajen
[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
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
Kurvy earned 20 total points
ID: 2545037
before the refresh try rebinding the data

0
 

Author Comment

by:tajen
ID: 2545225
I add datagrid1.rebind before datagrid1.refresh.
But datagrid1 still can not change the context after new SQL command was run.
0
 

Expert Comment

by:kthacker
ID: 2555928
Try setting datagrid1.datasource = nothing first.
0
 

Author Comment

by:tajen
ID: 2556882
I add set datagrid1.datasource=nothing in first line in command_click1()

But it still does not work
0
 

Expert Comment

by:kthacker
ID: 2559443
Close the recordset before you open it.  You will have to check the state first to see if it is open before you close it.

If rec.State = adStateOpen then
   rec.close
endif

I tried this and it worked.

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
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…
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…
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
Course of the Month8 days, 11 hours left to enroll

617 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