Solved

How to refresh datagrid with code

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

751 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