Solved

How to refresh datagrid with code

Posted on 2000-02-20
5
158 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
Comment Utility
before the refresh try rebinding the data

0
 

Author Comment

by:tajen
Comment Utility
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
Comment Utility
Try setting datagrid1.datasource = nothing first.
0
 

Author Comment

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

But it still does not work
0
 

Expert Comment

by:kthacker
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now