Solved

How to refresh datagrid with code

Posted on 2000-02-20
5
161 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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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 …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…

808 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