Re: DataGrid not showing programmatic changes

From: Jim Carlock (anonymous_at_127.0.0.1)
Date: 09/05/04


Date: Sat, 4 Sep 2004 22:14:32 -0400


----------------------------
5 STEPS
----------------------------
(1) To keep this simple start a new VB project rather than
messing with your current one just to keep it simple and
such.

(2) I tried to hook the following file up and couldn't get it to
connect, and I'm not sure it will connect through DAO.

Microsoft DataGrid Control 6.0 (SP6)
 - msdatgrd.ocx
(seems to be an ADODB grid only)

There's another data grid control that will connect, though:

Microsoft Data Bound Grid Control 5.0 (SP3)
 - DBGrid32.ocx
(this works with a Data1 control and can connect via
DAO)

There's mention of a grid in the DBGrid help file, but the
help file just mentions the Grid as something else,
without specifying which .ocx or .dll it is talking about.
I didn't pursue that at all but you might want to take a
look at that. I can't comment on it right at the moment.
Maybe someone else knows specifically which grid that
specifically is.

You can work with both grids at the same time, but I'll
talk about that at the end. It much simpler to work with
if you have two separate projects. It just starts to get a
tad confusing with both types in the same project.

Click on Project, then Components then select the right
grid for the project, as indicated above. If you're using
DAO then select the DBGrid32.ocx. Close the dialog
for Components.

(4) Drag and drop a DBGrid control to the form. Then
drag and drop a data control to the form. Set the following
properties of the Data1 control:
Connect: Access 2000;
DatabaseName: click on the ... button to browse to the source.
RecordSource: select the recordset or query you'd like to use
RecordsetType: I usually set this to 1 - Dynaset

(5) For the DBGrid, set the following properties:
DataMode: 0 - bound
DataSource: Data1

The data control is used here. I've tested changing the
RecordSource of the Data1 control to a different table and
then used the Data1.Refresh to update DBGrid. That works
just fine.

And finally a couple of comments:

--
(1) The DBGrid1.DataSource property cannot be changed at
Run-time. And I haven't messed with trying to connect
without using a Data control (Data1). But you can set the
Data1.DatabaseName by supplying a fully qualified file
path to another database, then set the Data1.RecordSource
to the table or query inside the .mdb. Then use
Data1.Refresh to update DBGrid1.
(2) If you're using events within class modules, declaring
DAO WithEvents will NOT work. It is not supported. You'd
have to use ADODB to declare variables WithEvents. This
might be good for you if you're working classes (class
modules). I don't have any information about how to put it
into use right at the moment, other than having you take a
look at Form Wizard, if you add a new form, click on the
Data Form Wizard and select class code, as opposed to one
of the other two options.
(3) The last thing that I will comment on, is if you're using
both DAO and ADO, you will need to differentiate the variables.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim adoDb As ADODB.Database
Dim adoRs As ADODB.Recordet
Hope that helps.
-- 
Jim Carlock
http://www.microcosmotalk.com/
Post replies to the newsgroup.
"Dene" wrote:
Jim, If you're following this thread, I have cracked it!! I
played around with the DataEnvironment connections for a
little bit putting loops in after each update that moved off the
record then back re-read it and ensured that the reread data was
what was written etc. Breakpoints and checking with the source
table through access. This fairly quickly proved it wasn't the
dataEnvironment misbehaving. All the results showed it behaving
perfectly with no anomolies. This only leaft the datagrid.
Obviously the refresh method wasn't causing the source data to
be reread. So instead of using the refresh method I reset the
datamember leaving the source and fields alone I.E.
dgCashbook.Datamember="CashBook"
BINGO!!! everything updated. :-))))
But I still went on to have a look at DAO none of the examples
in MSDN or your suggestions seemed to code properly. The
autocomplete thingy in the IDE wouldn't recognise the object
types or if it recognised the object types they didn't have
any properties or methods. I looked through the project
commponents and added every component I thought might have
something connected including the ADODC control but nothing
helped. What am I missing here to get DAO sorted?
Cheers
"Jim Carlock" wrote:
> Dene, do you have the help files for the DataEnvironment ?
> The VB 6 information seems to be very limited. I found
> source code in the past where I was using a
> DataEnvironment but some things are missing from it. If I
> remember correctly, I decided to use DAO for the majority
> of everything I needed to do. I did end up using some Data
> controls but those were hidden. Most of my projects today
> are all DAO with no controls, and I set up classes to manage
> everything. The ADO data controls worked great at times
> and usually I kept them hidden and worked with customized
> buttons to navigate if so needed.
>
> One of the thoughts that crossed my mind was to use use a
> Public form property to set the Data controls... such as:
>
> Private m_sCn As String
> Public Property Let Connection(ByVal sConn As String)
>   m_sCn = sConn
>   If Data1.State
>   Data1.Connect = sConn
>   Data2.Connect = sConn
> End Property
>
> If there are only three data connection strings, you could set
> up Public Properties and Events for each form, and then raise
> one event to call all the other events. This would be handy in
> changing the connection strings on each form. And it's pretty
> easy to get away with.
>
> I haven't worked with events all that much, but I can see
> where it could be put into use here.
>
> 'form1
> Public Enum enumRS_STATE
>   mctRSClosed = 0&
>   mctRSOpen = 1&
> End Enum
> Private msConnString As String
> Private msTable As String  'holds Table name or SQL
> Private rsState As enumRS_STATE
> Private rs As DAO.Recordset
> Public Event ConnectionChanged(sNewConn As String)
>
> Public Sub Form_ConnectionChanged(sNewConn As String)
>   'change the connection strings, reconnect here
>   If rsState = mctRSOpen Then
>     'close it
>     rs.Close
>     gdb.Close
>     rsState = mctRSClosed
>   End If
>   'change the connection
>   db.Connect = sNewConn
>   rs.Open msTable
> End Sub
>
> 'use a global variable or class variable to hold the new
> 'connect string, here would be a class named cAppClass
> Option Explicit
> Private msConn As String
> Private msNewConn As String
> Public Sub NewConnection(ByVal sNewConn As String)
>   Dim f As Form
>   For Each f In App.Forms
>     RaiseEvent f.ConnectionChanged sNewConn
>   Next f
> End Sub
>
> That should be pretty close to how an event might work for
> you.
> -- 
> Jim Carlock