Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > VBA / Office Integration > Excel > displaying data in a textbox after a combo box is selected


Reply
 
Thread Tools Display Modes
  #1  
Old 04-08-2012, 02:16 PM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default displaying data in a textbox after a combo box is selected


hi
i have a user for with a combobox and 5 text boxes, the combobox is liked to a sheet in my work book and after each list item are some details.
im wondering how when i select and item in the list in the combobox how the data next to it in the sheet can be displayed in the text box.
eg
list 1 Data 1 Data 2 Date 3
a 1 11 111
b 2 22 222
c 3 33 333
d 4 44 444

in userform combobox "a" is selected and in textbox 1 "1" textbox 2 "11" textbox 3 "111" ect ect
thank you
Reply With Quote
  #2  
Old 04-09-2012, 01:54 AM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

Let's assume your data is stored in a worksheet with code name "DATA" and “ComboBox1” is the name of your combo box in the userform “MyUserform”. You may therefore proceed as follows using the ComboBox change event:
Code:
Private Sub ComboBox1_Change()

'1) Retrieve the selected item value in ComboBox1 change event

Dim TheValue as String
TheValue = MyUserform.ComboBox1.Text

'2) Find the corresponding row in your worksheet assuming the key to look for is in first column of "DATA"

Dim TheSearch as Object
Dim TheRange As Range
Set TheRange = DATA.Range(DATA.Cells(1, 1), DATA.Cells(DATA.UsedRange.Rows.Count, 1))
Set TheSearch = TheRange.Find(What:=TheValue, After:=TheRange.Cells(TheRange.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

'3) Populate the textboxes, assuming the data to display is in columns 2, 3, and 4
MyUserform.TextBox1.Text = DATA.Cells(TheSearch.Row,2)
MyUserform.TextBox2.Text = DATA.Cells(TheSearch.Row,3)
MyUserform.TextBox3.Text = DATA.Cells(TheSearch.Row,4)

End Sub
Reply With Quote
  #3  
Old 04-10-2012, 10:24 AM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

when i use this code an error is returned on this line
Set TheRange = DATA.Range(DATA.Cells(1, 1), DATA.Cells(DATA.UsedRange.Rows.Count, 1))

i have changed the word "DATA" to the name of my sheet which is "JobList"
so looks like
Set TheRange = JobList.Range(JobList.Cells(3, 2), JobList.Cells(JobList.UsedRange.Rows.Count, 1))
is that correct?
thanks ash
Reply With Quote
  #4  
Old 04-10-2012, 11:31 AM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

As explained above, "DATA" is the code name (not worksheet name!) of your worksheet that needs to be replaced by your own worksheet code name. You can check the code names of your worksheets in the VBA editor on the left pane in "Microsoft Excel Objects".
Reply With Quote
  #5  
Old 04-10-2012, 12:50 PM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

i now have an error on this line
frmFindJob.cbofindcustomername.Text = Sheet2.Cells(TheSearch.Row, 4)
Reply With Quote
  #6  
Old 04-10-2012, 02:30 PM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

Probably because the search key could not be found. You can check if the search returns a match using:
Code:
If Not TheSearch is Nothing then
...
Else
   Msgbox "No match found! The Combobox / worksheet is erroneously populated!"
end If
Reply With Quote
  #7  
Old 04-10-2012, 03:01 PM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

still the same error line.
Reply With Quote
  #8  
Old 04-10-2012, 11:31 PM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

What is the error code?
Reply With Quote
  #9  
Old 04-11-2012, 10:00 AM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

rub time error '91'
object variable or with block variable not set
Reply With Quote
  #10  
Old 04-11-2012, 12:34 PM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

This is an error that occurs when you are using a variable property that is not defined.
In that case, this happens when TheSearch is nothing (e.g. using "TheSearch.Row" produces that error when no match was found, i.e. when TheSearch is Nothing).
See my post above.
Reply With Quote
  #11  
Old 04-11-2012, 02:20 PM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

i put that in the code and it still came up with the same error. here is the code ive put in.

Private Sub cbofindjobnumber_Change()

'1) Retrieve the selected item value in ComboBox1 change event

Dim TheValue As String
TheValue = frmFindJob.cbofindjobnumber.Text

'2) Find the corresponding row in your worksheet assuming the key to look for is in first column of "DATA"

Dim TheSearch As Object
Dim TheRange As Range
Set TheRange = Sheet2.Range(Sheet2.Cells(3, 2), Sheet2.Cells(Sheet2.UsedRange.Rows.Count, 1))
Set TheSearch = TheRange.Find(What:=TheValue, After:=TheRange.Cells(TheRange.Cells.Count), LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

'3) Populate the textboxes, assuming the data to display is in columns 2, 3, and 4
cbofindcustomername.Text = Sheet2.Cells(TheSearch.Row, 4)
txtfindcustomeraddress1.Text = Sheet2.Cells(TheSearch.Row, 5)
txtfindcustomeraddress2.Text = Sheet2.Cells(TheSearch.Row, 6)
If Not TheSearch Is Nothing Then
Else
MsgBox "No match found! The Combobox / worksheet is erroneously populated!"
End If
End Sub

there is data in the corresponding cells in columns 4,5 and 6
thank you
Reply With Quote
  #12  
Old 04-11-2012, 02:31 PM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

My best advise would that you first try to understand what the code does.


Then you would come to the conclusion that the use of TheSearch.Row has to be in the If Not TheSearch Is Nothing part of step 3):
Code:
If Not TheSearch Is Nothing Then
   cbofindcustomername.Text = Sheet2.Cells(TheSearch.Row, 4)
   txtfindcustomeraddress1.Text = Sheet2.Cells(TheSearch.Row, 5)
   txtfindcustomeraddress2.Text = Sheet2.Cells(TheSearch.Row, 6)
Else
   MsgBox "No match found! The Combobox / worksheet is erroneously populated!"
End If
This means as well your ComboBox / worksheet is indeed erroneously populated...
Reply With Quote
  #13  
Old 04-11-2012, 02:50 PM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

im sorry im really new to vb so learning as i go, ive put that code in but still comes back with the same error
Reply With Quote
  #14  
Old 04-11-2012, 03:03 PM
Casey_2's Avatar
Casey_2 Casey_2 is offline
Centurion
 
Join Date: Jul 2009
Location: Earth
Posts: 155
Default

Is TheRange correctly defined (starting at column 2 then ending at column 1) ?

Can you add the form reference "frmFindJob." in front of your three textboxes (cbofindcustomername, txtfindcustomeraddress1, txtfindcustomeraddress2) as you did with cbofindjobnumber?

Otherwise can you post (attachment) a minimal sample file that allows to reproduce your issue?
Reply With Quote
  #15  
Old 04-11-2012, 03:32 PM
ashgull80 ashgull80 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

i have sorted it, thank you very much for your help i was being an idiot i had changed the code of the sheet so it was not "sheet2"
thank you so much for your help sorry it took me so long to find my simple error
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->