Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a certain field from an array 3

Status
Not open for further replies.

Steve-vfp9user

Programmer
Feb 5, 2013
334
GB
Hello all

I am trying to select a certain field from an array list on a form.

Code:
SELECT * FROM myfile ORDER BY FORENAME INTO ARRAY mytechs

myfile has four fields in the order of FORENAME, SURNAME, POSITION, USERID

I only need the USERID which would then store itself in the field from a table I have selected from the forms builder (field from free table is called ALLOCATED).

So in other words, the form is presented, the list of users are on that form in a list box ordered by FORENAME, I double click on the relevant line I need, that records USERID field is then stored in my fields table called ALLOCATED.

The USERID and ALLOCATED fields are numeric.

I hope that makes sense but if not, please let me know.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
I hope that makes sense

Not completely ... but it's close.

Let me see if I have understood it right.

You have a form containing a listbox. The listbox is populated with an array (RowSourceType = 5), which in turn has four columns. The columns hold the forename, surname, position and user ID in that order. You want to arrange it such that when the user clicks on a row in the listbox, the user ID in that row updates a field named UserID in a table named Allocated.

Am I on the right track?

If so, you need to set the listbox's ControlSource to Allocated.UserID. Then set its BoundColumn to 4 (because that is the column containing the user ID). And finally set the BoundTo property to .T.

I think that would do what you want. However, you might not want all four fields to be visible in the listbox. You should have the ColumnCount property set to 4, and the ColumnWidth property to something "50,50,0,0". Note that this is a string. It will set the width of the first two columns (the forename and surname) to 50 pixels each, and it will hide the other two columns (set their widths to zero). If that's not what you want, adjust those four figures accordingly.

Let me know if I have correctly interpreted the requirements. If I haven't, I will try again.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi,

... or have a look at LIST and LISTINDEX e.g.

Code:
PROCEDURE cboNames.Click()
	LOCAL lcValue as Character
			
	lcValue = ALLTRIM(This.List(This.Listindex,4))

	...
			
ENDPROC

The behavior of lists and combos with numeric data confuses people. The key point is that, even when we think we're seeing numbers in a combo or list, we're actually seeing characters. Combos and lists cannot show numeric data. If you specify a RowSource that's numeric, FoxPro internally converts the data to character before displaying it.

From Hacker's Guide ...

Hence if you need numeric data to be returned be sure to convert them with VAL()


hth

MarK
 
If you only need the UserID then why not limit your array to that

Code:
SELECT UserID FROM myfile ORDER BY FORENAME INTO ARRAY mytechs

Chances are that you really want the surname and forename as well, to find a particular entry though

Code:
SELECT * FROM myfile ORDER BY FORENAME INTO ARRAY mytechs
for nRows = 1 to Alen(myTechs,1)
  ? myTechs(nRows,1) && forename
  ?? myTechs(nRows,2) && surname
  ?? myTechs(nRows,3) && position
  ?? myTechs(nRows,4) && UserID
Next



Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are !good for you.
 
Steve,

One more point. I think a combo box might be a better choice than a listbox. The programming would be identical. Same BoundTo, BoundColumn, ControlSource, etc. properties. But a combo box seems to me to be a better user interface in this situation.

Just a thought. Ignore it if you wish.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks all for the very quick responses.

Please allow me to ponder over them and I'll post back soon.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Hello all

Thanks again for the posts regarding my question.

So I went with Mike's suggestion of a Combo Box and yes, this looks more aesthetic. I created the following to populate the Combo Box:

Code:
SELECT forename, surname, position, userid FROM MYTABLE ORDER BY forename INTO ARRAY technicians

The CB ColumnWidths, ColumnLines etc are set.

I have two issues here:

1. When the form is loaded, the CB has a blank line and not the first record of the ARRAY, how do I show the first record?
2. When I select a record from the drop down list, it always selects the first record in the list (see below), why is this?

I put this code in a command button to check which record in the ARRAY had been selected and it's always the same record as mentioned in 2 above whereas I need the one I have selected obviously.

Code:
WAIT forename+" "+surname+" "+position+" "+ltrim(str(userid)) WINDOW

Any guidance would be very much appreciated.


Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Steve, to answer your first question, I think you need to set the combo's Style property to 2. Give it a try, and see if that solves the problem.

Re your second question, keep in mind that whatever the user does with the combo box does not affect the array in any way. Once the combo has been initialised and populated, the array is fixed. To find out which entry the user selected in the combo, you need to look at the combo's ListIndex property. If they select the second item, for example, ListIndex will contain 2.

Going further, if you look at [tt]this.List(this.ListIndex, 4)[/tt], that will tell you the actual user ID that was selected.

The variables that you are showing in the code in your Wait command have no connection with the contents of the combo, or even of the array. I suspect that's why the Wait window is not showing what you expect.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike

Thank you for the reply.

The Combo's style is already set to 2. I tried 0 also.

You mentioned:

The variables that you are showing in the code in your Wait command have no connection with the contents of the combo, or even of the array. I suspect that's why the Wait window is not showing what you expect.

I'm thinking I may have to try another avenue for what I'm trying to achieve and that is assign a system user to a job.

I'll keep looking and post back.

Much obliged.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Hi

I have two issues here:

1. When the form is loaded, the CB has a blank line and not the first record of the ARRAY, how do I show the first record?
2. When I select a record from the drop down list, it always selects the first record in the list (see below), why is this?

ad 1) In the INIT of your combobox event/method put THIS.Value = laArray[1,1] - the array must of course exist and it must exist within the scope
ad 2) Please check the code below

Code:
PUBLIC go_Form

go_Form=CREATEOBJECT("MyForm")
go_Form.Visible = .T.
go_Form.Show()

READ Events
CLOSE ALL 
CLEAR ALL 


DEFINE CLASS MyForm as Form
	AutoCenter = .T.
	Width = 420
	MinWidth = 420
	MaxWidth = 420
	Height = 360
	MinHeight = 360
	MaxHeight = 360
	ShowTips = .T.
	
	ADD OBJECT cboBox as ComboBox WITH Top = 30, Left = 6, Width = 240, Height = 24, ; 
			RowSourceType = 2, RowSource = "csrEmployees", Style = 2, Sorted = .T., ;
			Anchor = 3, ToolTipText = "Type or click down arrow"
			
	[highlight #FCE94F]		PROCEDURE cboBox.Init()
				This.Value = csrEmployees.cName
				
			ENDPROC 
[/highlight]
	ADD OBJECT txtName as TextBox WITH Top = 30, Left = 258, Width = 90, Height = 24, ReadOnly = .T., Anchor = 3, ;
		DisabledBackColor = RGB(230, 230, 230), ToolTipText = "Name"

	ADD OBJECT txtNumber as TextBox WITH Top = 60, Left = 258, Width = 90, Height = 24, ReadOnly = .T., Anchor = 3, ;
		Alignment = 1, DisabledBackColor = RGB(230, 230, 230), ToolTipText = "ID"									
	
	ADD OBJECT txtGender as TextBox WITH Top = 90, Left = 258, Width = 90, Height = 24, ReadOnly = .T., Anchor = 3, ;
		DisabledBackColor = RGB(230, 230, 230), ToolTipText = "Gender"

	ADD OBJECT txtHeight as TextBox WITH Top = 120, Left = 258, Width = 90, Height = 24, ReadOnly = .T., Anchor = 3, ;
		DisabledBackColor = RGB(230, 230, 230), Alignment = 1, ToolTipText = "Height"

	ADD OBJECT lblLabel as Label WITH Top = 6, Left = 6, Autosize = .T., ;
				Caption = "Name", Anchor = 3

	PROCEDURE Load()
		CREATE CURSOR csrNames (cName c(8),iNumber I, cGender c(1), nHeight N(5,2))
		INSERT INTO csrNames VALUES ('Karl', 123, 'M', 1.75)
		INSERT INTO csrNames VALUES ('Robert', 124, 'M', 1.80)
		INSERT INTO csrNames VALUES ('Luisa', 125, 'F', 1.70)
		INSERT INTO csrNames VALUES ('Megan', 126, 'F', 1.68)
		INSERT INTO csrNames VALUES ('Georges', 127, 'M', 1.82)
		INSERT INTO csrNames VALUES ('Kim', 128, 'F', 1.65)
		INSERT INTO csrNames VALUES ('Paul', 129, 'M', 1.85)
		INSERT INTO csrNames VALUES ('Carmen', 130, 'F', 1.54)
		INSERT INTO csrNames VALUES ('Elisa', 131, 'F', 1.68)
		INSERT INTO csrNames VALUES ('John', 132, 'M', 1.72)
		INSERT INTO csrNames VALUES ('Frank', 133, 'M', 1.90)
		
		SELECT * FROM csrNames ORDER BY 1 INTO CURSOR csrEmployees		
	ENDPROC

*!*	START of the procedure that feeds the TextBoxes with their values

	[highlight #FCE94F]PROCEDURE cboBox.Click()
		WITH ThisForm
			.txtName.Value = .cboBox.List(.cboBox.Listindex,1)
			.txtNumber.Value = .cboBox.List(.cboBox.Listindex,2)
			.txtGender.Value = .cboBox.List(.cboBox.Listindex,3)
			.txtHeight.Value = .cboBox.List(.cboBox.Listindex,4)
		ENDWITH 
	
	ENDPROC[/highlight] 

*!*	END of the feeding procedure
	
	PROCEDURE cboBox.InterActiveChange()
		This.Click()
	
	ENDPROC 
	
	PROCEDURE Destroy()
		USE
		CLEAR Events
		ThisForm.Release
	
	ENDPROC
ENDDEFINE

hth

MarK
 
Hi Mark

Massive thank you for posting the above. At this stage in my VFP quest as a relatively new user, I'm still creating my SCT's with the modi form formname so I wouldn't be able to use what you've mentioned.

Since posting to Mike, I have now created a form, added a grid box, populated that with the table details I require and that appears to be working.

Much appreciated.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Hi

I'm still creating my SCT's with the modi form formname so I wouldn't be able to use what you've mentioned

Yes you can - e.g. you click on your combobox on the form - RightClick and then choose Properties - doubleclick on the Click() method/event and add your code there.

hth

MarK
 
Ok Mark.
I’ll give it a go.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Mark,

It's probably too late to suggest this, but just going back to your first question above, it could be that the reason that the combo is initially blank is that you are sitting on a record in the Allocated table in which the UserID is blank. Given that the combo's control source points to Allocated.UserId, it follows that the combo will initially display whatever is in that field.

But I see this has been overtaken by events, given that you are now using a grid. That's fine. Let us know it it works out.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike

I checked the table and there are no blank records within.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Just an update on this thread.

I tried Mark’s coding and yes, it works (of course it does!)

However, I tried this with some changes to match my requirements and it I couldn’t get this to work in a command button on a form (sct) as it gave a few errors (not Marks’s coding) but I believe it was procedures you couldn’t use in this scenario.

Due to the time I have looked into this and the fact I don’t want to waste anyone else’s, I created a form, put a grid on it and populated that with my table data. That worked ok.

My initial thought on this was that by opening up another table on the same form that wasn’t linked may have lost focus when it returned to the form with the data being worked on.

All appears ok so much appreciated for those who contributed.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top