If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Applications & Tools > DataGrid Help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-04, 01:34
Hotaru_chan Hotaru_chan is offline
Registered User
 
Join Date: Dec 2003
Location: Philippines
Posts: 7
DataGrid Help

Hello.

Im quite new in programming with DataGrid in VB6. I have a program which imports a text file (comma-delimited value format) to a table in access 2002. i have a prob with binding the table to a DataGrid. It display the records unpermanently.


Dim adoRs As ADODB.Recordset

Set adoRs = New ADODB.Recordset
With adoRs
Set .ActiveConnection = adoCn
.CursorLocation = adUseServer
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Open strSQL, , , , adCmdText
End With

'bind recordset to the datagrid for display
With frmProcessAppForms.grdViewData
Set .DataSource = adoRs
.HoldFields
.ScrollBars = dbgBoth
.Refresh
End With

Is my DataGrid code right? Please Help.
Thanks.

Hotaru
Reply With Quote
  #2 (permalink)  
Old 01-04-04, 13:52
the_ace the_ace is offline
Registered User
 
Join Date: Jan 2004
Location: Sri Lanka
Posts: 4
technically there's nothing wrong in that code. But some questions do arise.
Is the ADO connection already active at this stage of the code ?
Also where is the Recordset's data retreval SQL ?

You cant just open an emplty recordset and expect to see data in the grid.

If the ADO Connection is active the folowing code should do the trick as long as it is in the same form as the Datagrid.

Dim adoRs As ADODB.Recordset

Set adoRs = New ADODB.Recordset
adoRs.open "SELECT * FROM some-table-in-your-database",adoCn, adOpenStatic, adLockReadOnly

'bind recordset to the datagrid for display
grdViewData.DataSource = adoRs
Reply With Quote
  #3 (permalink)  
Old 01-04-04, 21:57
Hotaru_chan Hotaru_chan is offline
Registered User
 
Join Date: Dec 2003
Location: Philippines
Posts: 7
Hm. Check this whole code:

Sub ViewData()

On Error GoTo ErrHandler

Dim adoCn As ADODB.Connection
Dim adoRs As ADODB.Recordset
Dim adoFld As ADODB.Field
Dim strCn As String
Dim strSQL As String

lblStatus.Caption = "Status: Viewing Data... "
txtViewData.Text = ""

'need to use microsoft access driver
'txtJetDriver.Text = {Micrsoft Access Driver (*.mdb)}
'txtDatabase.Text = Databasename.mdb

strCn = "Driver=" & txtJetDriver.Text & ";" & _
"DBQ=" & App.Path & "\" & txtDatabase.Text & ";" & _
"DefaultDir=" & App.Path & ";"

Set adoCn = New ADODB.Connection
adoCn.Open strCn '<< connection opened

'SQL statement
strSQL = "SELECT * FROM [" & txtTable.Text & "]"

Set adoRs = New ADODB.Recordset
adoRs.Open strSQL, adoCn, adOpenStatic, adLockReadOnly, adCmdText

'edit: bind recordset to the datagrid for display
Set grdViewData.DataSource = adoRs

While Not adoRs.EOF
For Each adoFld In adoRs.Fields
txtViewData.Text = txtViewData.Text & adoFld.Value & " | "
Next
txtViewData.Text = txtViewData.Text & vbCrLf
adoRs.MoveNext
Wend

GoTo ExitSub

ErrHandler:
lblStatus.Caption = "Status: Viewing Data - Error."
MsgBox "Error: " & Err.Number & vbCrLf & Err.Description

ExitSub:
If Not (adoRs Is Nothing) And (Not adoRs.State = 0) Then
lblStatus.Caption = "Status: Complete ... " & adoRs.RecordCount & " Records"
adoRs.Close
End If
Set adoRs = Nothing
adoCn.Close
Set adoCn = Nothing

End Sub

I think there's nothing wrong with that code except to the DataGrid wherein if i tried to bind the table, about 2 secs it displays the table and it scrolls down automatically O___o and then the table will disappear. I think i should use other data-binding objects.

Last edited by Hotaru_chan; 01-04-04 at 22:05.
Reply With Quote
  #4 (permalink)  
Old 01-04-04, 22:30
the_ace the_ace is offline
Registered User
 
Join Date: Jan 2004
Location: Sri Lanka
Posts: 4
all code there os ok excep this bit :

While Not adoRs.EOF
For Each adoFld In adoRs.Fields
txtViewData.Text = txtViewData.Text & adoFld.Value & " | "
Next
txtViewData.Text = txtViewData.Text & vbCrLf
adoRs.MoveNext
Wend

What are you trying to do in that while loop ?
Reply With Quote
  #5 (permalink)  
Old 01-04-04, 22:48
Hotaru_chan Hotaru_chan is offline
Registered User
 
Join Date: Dec 2003
Location: Philippines
Posts: 7
Oh. I have a Textbox that displays the imported text. Just to test if the text importing works.
Reply With Quote
  #6 (permalink)  
Old 01-04-04, 23:39
Hotaru_chan Hotaru_chan is offline
Registered User
 
Join Date: Dec 2003
Location: Philippines
Posts: 7
Here's the zipped proj.

Last edited by Hotaru_chan; 01-05-04 at 04:27.
Reply With Quote
  #7 (permalink)  
Old 01-04-04, 23:47
the_ace the_ace is offline
Registered User
 
Join Date: Jan 2004
Location: Sri Lanka
Posts: 4
OK I have the project. you can take it off the forum now.
Reply With Quote
  #8 (permalink)  
Old 01-04-04, 23:57
the_ace the_ace is offline
Registered User
 
Join Date: Jan 2004
Location: Sri Lanka
Posts: 4
ok! found your problem.
First of all if you're binding recodsets to a grid programmatically NEVER give the grid a layout at design time.
Do that programmatically as well.

Also because the grid is bound to the recordset, the moment you close the recodset the the grid loses it's data.

I have attached the fixed project.
Attached Files
File Type: zip test.zip (81.6 KB, 146 views)
Reply With Quote
  #9 (permalink)  
Old 01-05-04, 04:31
Hotaru_chan Hotaru_chan is offline
Registered User
 
Join Date: Dec 2003
Location: Philippines
Posts: 7
Sou da ne... well, thanks for that.
Im gonna fix my comp first before i DL that ^^
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On