Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2011, 09:56 AM
Authorized User
 
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing data between two recordsets into combo boxes

Hi,
I know my title may be confusing, but Here is the explanation. I have two database access tables whose "fieldname" has a one-to-many relationship with same "fieldname" in another table. When I refer to one of the items in the first table from a combobox, I want the corresponding values in the 2nd table to display in the other combo box. But so far, I have been having problems with this, could anybody help please??
Here is what I have so far:

'Assign Access data to an array
For lngBCount = 0 To lngBufferCount
varBufferArray(lngBCount) = rst1![Filter_Name]
rst1.MoveNext
Next lngBCount
cname.List() = varBufferArray

Dim strFindFilter As String

strFindFilter = "[Filter_Name] = '" & cname & "'"
With rst1
.FindFirst strFindFilter
If .NoMatch Then
MsgBox ("No Record found.")
Else
pore = .Fields(2).Value
End If
End With

Dim strFilterSize As String
strFilterSize = "[Filter_Name] = '" & cname & "'"
With rst2
.FindFirst strFilterSize
If .NoMatch Then
MsgBox ("No Record found.")
Else
For lngFCount = 0 To lngFilterCount
varFilterArray(lngFCount) = rst2![Filter_Size]
rst2.MoveNext
Next lngFCount
cfilter.List() = varFilterArray
End If
End With

I'm getting a "no current record" error

Thanks
 
Old June 23rd, 2011, 11:28 AM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

I think what you want is referred to as cascading combo boxes. This is where a combo box is filtered based on the selection (value) in another the first combo box.

If this is what you want then check out this:

Cascading Combo Boxes Using 2 Tables (Click Here)
Quote:
Demonstrates how to make combo boxes whose values are filtered by the value of other combo boxes (whose record source are two tables) in a cascade.
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015
 
Old June 23rd, 2011, 01:27 PM
Authorized User
 
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, thank you! I think that is exactly what I want, but the problem is the link u sent me only opens the table and form themselves. I was not able to look through the code behind the operation. Is there any other link that you can send to me, that would show the codes please?
 
Old June 23rd, 2011, 01:38 PM
Authorized User
 
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, I am writing this program in Microsoft Word VBA, so I'm not sure if the tools for creating the cascade is available to me
 
Old June 23rd, 2011, 02:16 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

Quote:
Originally Posted by bitex View Post
Also, I am writing this program in Microsoft Word VBA, so I'm not sure if the tools for creating the cascade is available to me
Ah ... this is in Word. I assumed you were building an Access form since this was in a Access related forum not Word.

I have never tried to do this in Word. I will see what I can figure out.
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015
 
Old June 23rd, 2011, 02:25 PM
Authorized User
 
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oh I am sorry, I chose Access VBA forum, because I have my database stored in the access database, but I am trying to retrieve the data into word form created in the word VBA. I have attached the complete code into this, so maybe that would help. I've been working on this since monday and still havent figured it out yet...thanks for your help!
Dim strAccessDir As String
Dim strDBName As String
Dim DAO As Object
Dim wks As DAO.Workspace
Dim dbs As DAO.Database
Dim rst1 As DAO.Recordset
Dim rst2 As DAO.Recordset

Dim varBufferArray() As Variant
'Dim medi As String

Dim varFilterArray() As Variant

'Dim cname As ComboBox
Dim cfilter As ComboBox
Dim pore As TextBox
Dim membrane As TextBox
Dim vendor As TextBox
Dim vendorNo As TextBox
Dim mediNo As TextBox

Dim lngBufferCount As Long
Dim lngBCount As Long

Dim lngFilterCount As Long
Dim lngFCount As Long

'Pick up path to Access database directory from Access SysCmd function
'Set appAccess = CreateObject("Access.Application")
strAccessDir = "E:"
strDBName = strAccessDir & "\Process Biochem\Adebiyi Aluko\dbTech.mdb"
Debug.Print "DBName: " & strDBName
'appAccess.Quit

'Set up reference to Access database
Set DAO = CreateObject("DAO.DBEngine.36")
Set wks = DAO.Workspaces(0)
Set dbs = wks.OpenDatabase(strDBName)
'Set cname = cmbCName.Text
'Set cfilter = cmbFilter
Set pore = txtPore
Set membrane = txtMembrane
Set vendor = txtVendorPure
Set vendorNo = txtVendorNo
Set mediNo = txtMEDIPure


'Retrieve Category info from table
Set rst1 = dbs.OpenRecordset("Table2-3", dbOpenDynaset)
rst1.MoveLast
lngBufferCount = rst1.RecordCount - 1
ReDim varBufferArray(lngBufferCount) As Variant
Debug.Print "Number of filters in table: " & lngBufferCount
rst1.MoveFirst

'Retrieve Category info from table
Set rst2 = dbs.OpenRecordset("Table2-3b", dbOpenDynaset)
rst2.MoveLast
lngFilterCount = rst2.RecordCount - 1
ReDim varFilterArray(lngFilterCount) As Variant
Debug.Print "Number of filter sizes in table: " & lngFilterCount
rst2.MoveFirst


Dim strFindFilter As String

strFindFilter = "[Filter_Name] = '" & cmbCName.Text & "'"
With rst1
.FindFirst strFindFilter
If .NoMatch Then
MsgBox ("No Record found.")
Else
membrane = .Fields(2).Value
pore = .Fields(3).Value
vendor = .Fields(4).Value
End If
End With


Dim qry As String
qry = "SELECT Filter_Size FROM Table2-3b WHERE Filter_Name = '" & cmbCName.Text & "'; "

rst2.MoveFirst

While Not rst2.EOF
cmbFilter = (rst2.Fields("Filter_Size"))
rst2.MoveNext
Wend
Set rst2 = Nothing


the bold part is the main problem. I want it to give me values that correspond to the chosen data from cmbCName.text, but instead of specific values, it's giving me the whole data field of "Filter_Size" in the database
 
Old June 23rd, 2011, 02:50 PM
Authorized User
 
Join Date: Jun 2011
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,
I have finally figured out the problem!!! I was suppose to put the query into the recordset, as opposed to putting the whole table into it. Then I was supposed to use the "additem" command to add the data into the combobox. Thanks for willing to help though!!

Wow, it took me 4 days to figure out the problem
 
Old June 23rd, 2011, 04:12 PM
Friend of Wrox
 
Join Date: Sep 2010
Posts: 245
Thanks: 5
Thanked 24 Times in 23 Posts
Default

Thanks for the update.

Great job getting it figured out.
__________________
Boyd Trimmell aka HiTechCoach (.com)
Microsoft Access MVP Alumni 2010-2015





Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying data in a tree view from combo boxes Gini Visual Studio 2008 0 June 25th, 2008 01:52 AM
Multiple combo boxes, one data set. jbenson001 VB.NET 2002/2003 Basics 6 March 23rd, 2005 10:39 AM
Combo boxes socoolbrewster Access 1 March 4th, 2004 09:28 AM
Combo Boxes Louisa Beginning VB 6 2 September 10th, 2003 09:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.