Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid not show datas

Status
Not open for further replies.

Hira

Programmer
Nov 12, 2002
9
0
0
JP
Hi ! I am Hira.
I am learning how to use DataGrid control.
I want to show datas from MS Access table on the DataGrid.
But I could't do that.

Please give me some advices for resolving this problem.
I have confirmed I can connect my VB application with Access DB. My code is as bellow.

Private Sub Form_Load()

Dim MySql As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

On Error GoTo Err_Form_Load

cn.ConnectionString = "XXXXXX"

cn.Open

MySql = "SELECT MachineNumber FROM MachineData"

rs.Open MySql, cn, adOpenStatic, adLockOptimistic

Set dbgTrouble.DataSource = rs


Exit_Form_Load:

Exit Sub

Err_Form_Load:
MsgBox (Err.Number & Chr(13) & Err.Description)
End

End Sub

 
Prior to the rs.Open, add:

rs.Cursorlocation = adUseClient [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
You declared your variables under Form_Load Event

Private Sub Form_Load()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
.....

right? That's not right, because your variables scope will be just in that Procedure/Event. Try to move your variables under the General Declaration (Top most of your Code Window), so that it will be available and can be used by use other controls event.
 
Hi CCLINT !
Thank you for your reply.

After I added a code you wrote in your reply,
my DataGrid showed some datas from Access Database.

Thanks
Hira


 
Hi fulbertgil !

Thank you for your reply.

I have tested DataGrid in my project that has only form_load event code, So I declared the variables
in event code.

I will appreciate your reply.

Thanks
Hira

 
Hira,
You're right! It is normally good practice to declare variables with the minimum scope.
If you just use a variable in a procedure then dim in the procedure
Only declare as Form level variable if you want to use it in other procedures
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
But with the ADO recordset variable, if you want to use the ADO events, you will need to declare it at the class/form class level using W"ithEvents".

Also,
Declaring an object variable at class level with the "New" keyword is not such a good idea.

Best, would ne to declare it with-out the "New" keyword and then in the form load event use the "Set VarName = New ObjectName" statement. [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Hi CCLINT

I understand what you wrote.
I think you are right.

Thanks a lot.

I was only interisted in learning how to use
DataGrid.
So I did not care for other things.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top