I am using Access 2007 for a prototype application.I have a two database testUi.accdb (which
contain all of my forms) and testDb.mdb (which contain my database).
Now I want to use a datasheet in one of my subform.
Can anybody help me with these questions?
- Can I manually build a datasheet without table?
- How can i bind data with this datasheet using DAO or ADO by VBA Code
- Is it possible to loop a datasheet using VBA Code ? get or set value for each rows?
- If above are not possible then what are the alternate way
to use datasheet like grid in Access?
1) Datasheet forms can only be bound to DAO recordset objects or ADO Recordset objects. You can bind them to DAO recordset objects implicitly by setting the RecordSource or you can do it manually in code as shown in #2. You can only bind to an ADO recordset using code and then some of the Datasheet’s default options like filtering and sorting will not work.
2) I know this is overly simplified but… On the form’s Load Event: Set Me.Recordset = rs (rs is your recordset object)
3) You cannot loop through the datasheet inserting row objects and data like you can on many grid controls.
4) You could use Microsoft’s ActiveX controls such as the Grid Control or ListView Control.
One way you could possibly accomplish this is to use a “fabricated ADO recordset” and then bind the form to that recordset. This would be using the options I laid out in #1 and #2.
In any case, an Access Datasheet form does not dynamically add and configure enough cells/columns to display data like many grid controls do. This means you’ll need to have enough textboxes on the form before hand, and then use code to bind them to the fields in your DAO or ADO recordset. Any columns you don’t need will have to be hidden using code. Label captions will have to be filled out using code so they make proper column headers. In short, while the datasheet view is actually very powerful, it simply isn’t a Grid Control.
If you’ve ever used .NET it would probably be reasonably simple (although not trivial) to create an AddIn or a dll that would give you what you are looking for. The only problem is that .NET’s DataGridView cannot be bound to a DAO or ADO recordset so you’ll have to write code to take a DAO or ADO recordset and “translate” that recordset to an ADO.NET DataTable. Alternately you could write is so that your .NET form does data access by taking database and query information and using that to retrieve the desired data.
Edit1
In response to your comments below, here’s some example code. I think you need to create an explicit recordset object, which you’re not doing in the comment below.