Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default RowSource for User Form

I am venturing from an experienced Access/VBA developer to doing some work in
Excel with VBA. I need some help with how to accomplish a specific task.
The objective is that the user needs to enter an employee name and have it
return the Employee Number and Department. This data is in an Access
database table.
I want to use a combo box to make it easy for the user to locate the
employee name and have it fill the cells in the row where the employee data
is.

In looking at how Excel combo boxes work, it says the combo rowsource has to
be a worksheet range. I was hoping a could use a recordset like you do in
Access. But since that does not seem to be available, I came up with this
approach:

Add a new worksheet.
Create the recordset that contains the employee table from the Access database
Use the CopyRecordsetFrom method to copy the row from the recordset into the
new worksheet.
Use the range the data is now in as my rowsource for the combo.
When the user selects a row from the combo, load the other columns' data
into the cells where the data needs to go.

The question is, is this a reasonable approach or is there a better method
to do this?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default RowSource for User Form

Ok....you should be able to fill a combo box from an access dataset....first
add a reference to Microsoft ActiveX Data Object (MDAC).

The following code creates...opens data source...returns dataset....and
closes the data source (you will have to make minor corrections to suit your
needs but should point you in the write direction). You can also use the the
same techique to return your look u information and then put it on a form or
in Cells.

Private m_strCnn As String
Private m_ADOConnection As New ADODB.Connection
Private m_ADORecordSet As New ADODB.Recordset
Private m_StoredProcedure As String
Private m_sSql As String


Public Function openConn()
On Error GoTo myErr:

m_strCnn = "provider=Microsoft.Jet.OLEDB.4.0;data source=" &
ThisWorkbook.CB & ";"
If m_ADOConnection.State = 0 Then
m_ADOConnection.Open (m_strCnn)
End If

Exit Function

myErr:

MsgBox (Error$)

End Function

Public Function closeConn() 'This function closes a valid ADO connection

On Error GoTo myErr:


m_ADOConnection.Close

Exit Function

myErr:

End Function

Public Function getRecordSet(ByVal SqlCommand As String) As ADODB.Recordset

'Open Connection
Call openConn

'This function returns a record set from a valid Open Connection
On Error GoTo myErr:
Set getRecordSet = New ADODB.Recordset
Call getRecordSet.Open(SqlCommand, m_ADOConnection, adOpenForwardOnly,
adLockReadOnly)



Exit Function

myErr:

MsgBox (Error$)
End Function


The Following code uses the return data set and applies it to the combo box.

Dim myRecord As New ADODB.Recordset
Set myRecord = Conn.getRecordSet("Exec myQRY")

myRecord.MoveFirst

While myRecord.EOF < True
myForm.ComboBox1.AddItem (myRecord.Fields(0).Value)
myRecord.MoveNext
Wend



"Klatuu" wrote:

I am venturing from an experienced Access/VBA developer to doing some work in
Excel with VBA. I need some help with how to accomplish a specific task.
The objective is that the user needs to enter an employee name and have it
return the Employee Number and Department. This data is in an Access
database table.
I want to use a combo box to make it easy for the user to locate the
employee name and have it fill the cells in the row where the employee data
is.

In looking at how Excel combo boxes work, it says the combo rowsource has to
be a worksheet range. I was hoping a could use a recordset like you do in
Access. But since that does not seem to be available, I came up with this
approach:

Add a new worksheet.
Create the recordset that contains the employee table from the Access database
Use the CopyRecordsetFrom method to copy the row from the recordset into the
new worksheet.
Use the range the data is now in as my rowsource for the combo.
When the user selects a row from the combo, load the other columns' data
into the cells where the data needs to go.

The question is, is this a reasonable approach or is there a better method
to do this?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default RowSource for User Form

Thanks for the input, Jim. I do have one question (or 2), First, I am
familiar with adding items to an Access combo, but I did not see that an
Excel combo can accept a value list. The help file says it has to be a
range, so I am a little confused on that.

Second. I see your code for instantiating a recordset, but I wonder what is
the difference between what you propose and the code below that I have
already tested.

Dim wrkJet As Workspace
Dim dbf As DAO.Database
Dim rst As DAO.Recordset

On Error GoTo UserForm_Activate_Error

Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set dbs = wrkJet.OpenDatabase _
("\\rsltx1-bm01\busmgmt\Dev CISCMS\ciscmsdata.mdb")
Set rst = dbs.OpenRecordset("Select * from tbllkemployee;")

Thanks again for your response.
"Jim F" wrote:

Ok....you should be able to fill a combo box from an access dataset....first
add a reference to Microsoft ActiveX Data Object (MDAC).

The following code creates...opens data source...returns dataset....and
closes the data source (you will have to make minor corrections to suit your
needs but should point you in the write direction). You can also use the the
same techique to return your look u information and then put it on a form or
in Cells.

Private m_strCnn As String
Private m_ADOConnection As New ADODB.Connection
Private m_ADORecordSet As New ADODB.Recordset
Private m_StoredProcedure As String
Private m_sSql As String


Public Function openConn()
On Error GoTo myErr:

m_strCnn = "provider=Microsoft.Jet.OLEDB.4.0;data source=" &
ThisWorkbook.CB & ";"
If m_ADOConnection.State = 0 Then
m_ADOConnection.Open (m_strCnn)
End If

Exit Function

myErr:

MsgBox (Error$)

End Function

Public Function closeConn() 'This function closes a valid ADO connection

On Error GoTo myErr:


m_ADOConnection.Close

Exit Function

myErr:

End Function

Public Function getRecordSet(ByVal SqlCommand As String) As ADODB.Recordset

'Open Connection
Call openConn

'This function returns a record set from a valid Open Connection
On Error GoTo myErr:
Set getRecordSet = New ADODB.Recordset
Call getRecordSet.Open(SqlCommand, m_ADOConnection, adOpenForwardOnly,
adLockReadOnly)



Exit Function

myErr:

MsgBox (Error$)
End Function


The Following code uses the return data set and applies it to the combo box.

Dim myRecord As New ADODB.Recordset
Set myRecord = Conn.getRecordSet("Exec myQRY")

myRecord.MoveFirst

While myRecord.EOF < True
myForm.ComboBox1.AddItem (myRecord.Fields(0).Value)
myRecord.MoveNext
Wend



"Klatuu" wrote:

I am venturing from an experienced Access/VBA developer to doing some work in
Excel with VBA. I need some help with how to accomplish a specific task.
The objective is that the user needs to enter an employee name and have it
return the Employee Number and Department. This data is in an Access
database table.
I want to use a combo box to make it easy for the user to locate the
employee name and have it fill the cells in the row where the employee data
is.

In looking at how Excel combo boxes work, it says the combo rowsource has to
be a worksheet range. I was hoping a could use a recordset like you do in
Access. But since that does not seem to be available, I came up with this
approach:

Add a new worksheet.
Create the recordset that contains the employee table from the Access database
Use the CopyRecordsetFrom method to copy the row from the recordset into the
new worksheet.
Use the range the data is now in as my rowsource for the combo.
When the user selects a row from the combo, load the other columns' data
into the cells where the data needs to go.

The question is, is this a reasonable approach or is there a better method
to do this?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default RowSource for User Form

rst.MoveFirst

while rst.EOF = false
me.combobox1.additem(rst.Fields(0).Value) rst.MoveNext
Wend

whe 'Zero is the index of the field you want binded to the combobox.

Not Sure if this answers your question?!?!?


"Klatuu" wrote:

Thanks for the input, Jim. I do have one question (or 2), First, I am
familiar with adding items to an Access combo, but I did not see that an
Excel combo can accept a value list. The help file says it has to be a
range, so I am a little confused on that.

Second. I see your code for instantiating a recordset, but I wonder what is
the difference between what you propose and the code below that I have
already tested.

Dim wrkJet As Workspace
Dim dbf As DAO.Database
Dim rst As DAO.Recordset

On Error GoTo UserForm_Activate_Error

Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set dbs = wrkJet.OpenDatabase _
("\\rsltx1-bm01\busmgmt\Dev CISCMS\ciscmsdata.mdb")
Set rst = dbs.OpenRecordset("Select * from tbllkemployee;")

Thanks again for your response.
"Jim F" wrote:

Ok....you should be able to fill a combo box from an access dataset....first
add a reference to Microsoft ActiveX Data Object (MDAC).

The following code creates...opens data source...returns dataset....and
closes the data source (you will have to make minor corrections to suit your
needs but should point you in the write direction). You can also use the the
same techique to return your look u information and then put it on a form or
in Cells.

Private m_strCnn As String
Private m_ADOConnection As New ADODB.Connection
Private m_ADORecordSet As New ADODB.Recordset
Private m_StoredProcedure As String
Private m_sSql As String


Public Function openConn()
On Error GoTo myErr:

m_strCnn = "provider=Microsoft.Jet.OLEDB.4.0;data source=" &
ThisWorkbook.CB & ";"
If m_ADOConnection.State = 0 Then
m_ADOConnection.Open (m_strCnn)
End If

Exit Function

myErr:

MsgBox (Error$)

End Function

Public Function closeConn() 'This function closes a valid ADO connection

On Error GoTo myErr:


m_ADOConnection.Close

Exit Function

myErr:

End Function

Public Function getRecordSet(ByVal SqlCommand As String) As ADODB.Recordset

'Open Connection
Call openConn

'This function returns a record set from a valid Open Connection
On Error GoTo myErr:
Set getRecordSet = New ADODB.Recordset
Call getRecordSet.Open(SqlCommand, m_ADOConnection, adOpenForwardOnly,
adLockReadOnly)



Exit Function

myErr:

MsgBox (Error$)
End Function


The Following code uses the return data set and applies it to the combo box.

Dim myRecord As New ADODB.Recordset
Set myRecord = Conn.getRecordSet("Exec myQRY")

myRecord.MoveFirst

While myRecord.EOF < True
myForm.ComboBox1.AddItem (myRecord.Fields(0).Value)
myRecord.MoveNext
Wend



"Klatuu" wrote:

I am venturing from an experienced Access/VBA developer to doing some work in
Excel with VBA. I need some help with how to accomplish a specific task.
The objective is that the user needs to enter an employee name and have it
return the Employee Number and Department. This data is in an Access
database table.
I want to use a combo box to make it easy for the user to locate the
employee name and have it fill the cells in the row where the employee data
is.

In looking at how Excel combo boxes work, it says the combo rowsource has to
be a worksheet range. I was hoping a could use a recordset like you do in
Access. But since that does not seem to be available, I came up with this
approach:

Add a new worksheet.
Create the recordset that contains the employee table from the Access database
Use the CopyRecordsetFrom method to copy the row from the recordset into the
new worksheet.
Use the range the data is now in as my rowsource for the combo.
When the user selects a row from the combo, load the other columns' data
into the cells where the data needs to go.

The question is, is this a reasonable approach or is there a better method
to do this?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default RowSource for User Form

Sorry, Jim, maybe I was not clear. manipulating a recordset is not a problem
for me. My question was, will an Excel combo box accept a value list like an
Access combo? Also, in you code, you are dealing with a one column combo, I
neglected to point out I have 3 columns.

Also, in my previous post, I included code I am currently using to create my
recordset. It is not the same as what you posted. My question on that was
whether my method will work, or is the method you posted neccesary to be able
to populate the combo box?

"Jim F" wrote:

rst.MoveFirst

while rst.EOF = false
me.combobox1.additem(rst.Fields(0).Value) rst.MoveNext
Wend

whe 'Zero is the index of the field you want binded to the combobox.

Not Sure if this answers your question?!?!?


"Klatuu" wrote:

Thanks for the input, Jim. I do have one question (or 2), First, I am
familiar with adding items to an Access combo, but I did not see that an
Excel combo can accept a value list. The help file says it has to be a
range, so I am a little confused on that.

Second. I see your code for instantiating a recordset, but I wonder what is
the difference between what you propose and the code below that I have
already tested.

Dim wrkJet As Workspace
Dim dbf As DAO.Database
Dim rst As DAO.Recordset

On Error GoTo UserForm_Activate_Error

Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set dbs = wrkJet.OpenDatabase _
("\\rsltx1-bm01\busmgmt\Dev CISCMS\ciscmsdata.mdb")
Set rst = dbs.OpenRecordset("Select * from tbllkemployee;")

Thanks again for your response.
"Jim F" wrote:

Ok....you should be able to fill a combo box from an access dataset....first
add a reference to Microsoft ActiveX Data Object (MDAC).

The following code creates...opens data source...returns dataset....and
closes the data source (you will have to make minor corrections to suit your
needs but should point you in the write direction). You can also use the the
same techique to return your look u information and then put it on a form or
in Cells.

Private m_strCnn As String
Private m_ADOConnection As New ADODB.Connection
Private m_ADORecordSet As New ADODB.Recordset
Private m_StoredProcedure As String
Private m_sSql As String


Public Function openConn()
On Error GoTo myErr:

m_strCnn = "provider=Microsoft.Jet.OLEDB.4.0;data source=" &
ThisWorkbook.CB & ";"
If m_ADOConnection.State = 0 Then
m_ADOConnection.Open (m_strCnn)
End If

Exit Function

myErr:

MsgBox (Error$)

End Function

Public Function closeConn() 'This function closes a valid ADO connection

On Error GoTo myErr:


m_ADOConnection.Close

Exit Function

myErr:

End Function

Public Function getRecordSet(ByVal SqlCommand As String) As ADODB.Recordset

'Open Connection
Call openConn

'This function returns a record set from a valid Open Connection
On Error GoTo myErr:
Set getRecordSet = New ADODB.Recordset
Call getRecordSet.Open(SqlCommand, m_ADOConnection, adOpenForwardOnly,
adLockReadOnly)



Exit Function

myErr:

MsgBox (Error$)
End Function


The Following code uses the return data set and applies it to the combo box.

Dim myRecord As New ADODB.Recordset
Set myRecord = Conn.getRecordSet("Exec myQRY")

myRecord.MoveFirst

While myRecord.EOF < True
myForm.ComboBox1.AddItem (myRecord.Fields(0).Value)
myRecord.MoveNext
Wend



"Klatuu" wrote:

I am venturing from an experienced Access/VBA developer to doing some work in
Excel with VBA. I need some help with how to accomplish a specific task.
The objective is that the user needs to enter an employee name and have it
return the Employee Number and Department. This data is in an Access
database table.
I want to use a combo box to make it easy for the user to locate the
employee name and have it fill the cells in the row where the employee data
is.

In looking at how Excel combo boxes work, it says the combo rowsource has to
be a worksheet range. I was hoping a could use a recordset like you do in
Access. But since that does not seem to be available, I came up with this
approach:

Add a new worksheet.
Create the recordset that contains the employee table from the Access database
Use the CopyRecordsetFrom method to copy the row from the recordset into the
new worksheet.
Use the range the data is now in as my rowsource for the combo.
When the user selects a row from the combo, load the other columns' data
into the cells where the data needs to go.

The question is, is this a reasonable approach or is there a better method
to do this?



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default RowSource for User Form

Ok...I think i understand the question. You want the combobox to display the
employee name but pass a different value like the employee id...correct? if
so...Excel combo boxes do not have (as far as I know) a way to tie a "value"
column to it.

As for the way you get the record set...I think the code you have will work.

"Klatuu" wrote:

Sorry, Jim, maybe I was not clear. manipulating a recordset is not a problem
for me. My question was, will an Excel combo box accept a value list like an
Access combo? Also, in you code, you are dealing with a one column combo, I
neglected to point out I have 3 columns.

Also, in my previous post, I included code I am currently using to create my
recordset. It is not the same as what you posted. My question on that was
whether my method will work, or is the method you posted neccesary to be able
to populate the combo box?

"Jim F" wrote:

rst.MoveFirst

while rst.EOF = false
me.combobox1.additem(rst.Fields(0).Value) rst.MoveNext
Wend

whe 'Zero is the index of the field you want binded to the combobox.

Not Sure if this answers your question?!?!?


"Klatuu" wrote:

Thanks for the input, Jim. I do have one question (or 2), First, I am
familiar with adding items to an Access combo, but I did not see that an
Excel combo can accept a value list. The help file says it has to be a
range, so I am a little confused on that.

Second. I see your code for instantiating a recordset, but I wonder what is
the difference between what you propose and the code below that I have
already tested.

Dim wrkJet As Workspace
Dim dbf As DAO.Database
Dim rst As DAO.Recordset

On Error GoTo UserForm_Activate_Error

Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
Set dbs = wrkJet.OpenDatabase _
("\\rsltx1-bm01\busmgmt\Dev CISCMS\ciscmsdata.mdb")
Set rst = dbs.OpenRecordset("Select * from tbllkemployee;")

Thanks again for your response.
"Jim F" wrote:

Ok....you should be able to fill a combo box from an access dataset....first
add a reference to Microsoft ActiveX Data Object (MDAC).

The following code creates...opens data source...returns dataset....and
closes the data source (you will have to make minor corrections to suit your
needs but should point you in the write direction). You can also use the the
same techique to return your look u information and then put it on a form or
in Cells.

Private m_strCnn As String
Private m_ADOConnection As New ADODB.Connection
Private m_ADORecordSet As New ADODB.Recordset
Private m_StoredProcedure As String
Private m_sSql As String


Public Function openConn()
On Error GoTo myErr:

m_strCnn = "provider=Microsoft.Jet.OLEDB.4.0;data source=" &
ThisWorkbook.CB & ";"
If m_ADOConnection.State = 0 Then
m_ADOConnection.Open (m_strCnn)
End If

Exit Function

myErr:

MsgBox (Error$)

End Function

Public Function closeConn() 'This function closes a valid ADO connection

On Error GoTo myErr:


m_ADOConnection.Close

Exit Function

myErr:

End Function

Public Function getRecordSet(ByVal SqlCommand As String) As ADODB.Recordset

'Open Connection
Call openConn

'This function returns a record set from a valid Open Connection
On Error GoTo myErr:
Set getRecordSet = New ADODB.Recordset
Call getRecordSet.Open(SqlCommand, m_ADOConnection, adOpenForwardOnly,
adLockReadOnly)



Exit Function

myErr:

MsgBox (Error$)
End Function


The Following code uses the return data set and applies it to the combo box.

Dim myRecord As New ADODB.Recordset
Set myRecord = Conn.getRecordSet("Exec myQRY")

myRecord.MoveFirst

While myRecord.EOF < True
myForm.ComboBox1.AddItem (myRecord.Fields(0).Value)
myRecord.MoveNext
Wend



"Klatuu" wrote:

I am venturing from an experienced Access/VBA developer to doing some work in
Excel with VBA. I need some help with how to accomplish a specific task.
The objective is that the user needs to enter an employee name and have it
return the Employee Number and Department. This data is in an Access
database table.
I want to use a combo box to make it easy for the user to locate the
employee name and have it fill the cells in the row where the employee data
is.

In looking at how Excel combo boxes work, it says the combo rowsource has to
be a worksheet range. I was hoping a could use a recordset like you do in
Access. But since that does not seem to be available, I came up with this
approach:

Add a new worksheet.
Create the recordset that contains the employee table from the Access database
Use the CopyRecordsetFrom method to copy the row from the recordset into the
new worksheet.
Use the range the data is now in as my rowsource for the combo.
When the user selects a row from the combo, load the other columns' data
into the cells where the data needs to go.

The question is, is this a reasonable approach or is there a better method
to do this?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I fill a cell in a user form from a selection on same form? Terry Tipsy Excel Discussion (Misc queries) 4 June 11th 07 02:59 PM
How can Rowsource be used for a combobox on a form? Matt[_33_] Excel Programming 5 October 25th 05 03:00 AM
How to: User Form to assign a user defined range to a macro variab TrevTrav Excel Programming 1 March 22nd 05 07:57 PM
I am looking to see if anybody has an equivalant user form to Outlooks CONTACT form BruceJ[_2_] Excel Programming 2 October 15th 03 05:28 PM


All times are GMT +1. The time now is 06:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"