Creating an array list for a Combobox

Creating an array list for a Combobox

Postby Rick Lipkin » Mon Apr 02, 2012 11:05 pm

To all

I would think this exercise was fairly straight forward but has not turned out that way .. I have a table I want to read an Employee name into an array then use that array with the ITEMS clause to be able to chose my name in a ComboBox.

Unfortunately, when I create the array and use it like ITEMS aTech .. the computer GPFs and I do not know why .. Here is a portion of my code.

Please let me know what I am doing wrong ?

Thanks
Rick Lipkin

Code: Select all  Expand view

// create the Tech pull down array

cSql := "Select [First Name], [Last Name], [Service Calls], [Shop Repairs],[Employee Number] "
cSql += "From Employees Order by [First Name]"

oRsEmp := TOleAuto():New( "ADODB.Recordset" )
oRsEmp:CursorType     := 1        // opendkeyset
oRsEmp:CursorLocation := 3        // local cache
oRsEmp:LockType       := 3        // lockoportunistic

TRY
   oRsEmp:Open( cSQL,xCONNECT )
CATCH oErr
   MsgInfo( "Error in Opening EMPLOYEES table" )
*   _CleanUP()
   RETURN(.F.)
END TRY

aTech := {}
aLine := { "Blank" }
AAdd( aTech, aLine )

xBrowse( aTech )

If oRsEmp:eof
Else
   oRsEmp:MoveFirst()
   Do While .not. oRsEmp:eof

      If oRsEmp:Fields("Shop Repairs"):Value  = .t. .or.;
         oRsEmp:Fields("Service Calls"):Value = .t.
      Else
         oRsEmp:MoveNext()
         Loop
      Endif

      If empty( oRsEmp:Fields("First Name"):Value )
         cFname := "Unknown"
      Else
         cFname := alltrim(oRsEmp:Fields("First Name"):Value)
      Endif
      If empty( oRsEmp:Fields("Last Name"):Value )
         cLname := "Unknown"
      Else
         cLname := alltrim(oRsEmp:Fields("Last Name"):Value)
      Endif

      cName := cFname+" "+cLname

      aLine := { cName }
      AAdd( aTech, aLine )

      oRsEmp:MoveNext()

   End Do
Endif

oRsEmp:CLose()

xBrowse( atech ) // array is created corectly

...
...

cTech := space(50)

REDEFINE COMBOBOX oTech  var cTech   ID 127 of oWorkB  ;
               ITEMS aTech

 
User avatar
Rick Lipkin
 
Posts: 2665
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Creating an array list for a Combobox

Postby fraxzi » Tue Apr 03, 2012 12:28 am

Dear Rick,

i'm not sure if this will work on you but in my case it does.

after redefining the combobox,

Code: Select all  Expand view

...
oTech:SetItems( aTech /*{'item1', 'item2', 'item3'}*/, .F. /*to avoid executing bChange on oDlg Init */)
cTech := oTech:aItems[ 1 ]
...
 
Kind Regards,
Frances

Fivewin for xHarbour v18.07
xHarbour v1.2.3.x
BCC 7.3 + PellesC8 ( Resource Compiler only)
ADS 10.1 / MariaDB
Crystal Reports 8.5/9.23 DE
xMate v1.15
User avatar
fraxzi
 
Posts: 811
Joined: Tue May 06, 2008 4:28 am
Location: Philippines

Re: Creating an array list for a Combobox

Postby ADutheil » Tue Apr 03, 2012 1:08 am

Hi Rick,

The way you create aTech results in an array of arrays { { "Blank" }, {cName1}, {cName2} ...} when it should be {"Blank",cName1,cName2...}

Why don´t you use a where clause in you SQL statment to avoid the next code? Is it not possible with ADO?

Code: Select all  Expand view
   If oRsEmp:Fields("Shop Repairs"):Value  = .t. .or.;
         oRsEmp:Fields("Service Calls"):Value = .t.
      Else
         oRsEmp:MoveNext()
         Loop
      Endif
 
Regards,

André Dutheil
FWH 13.04 + HB 3.2 + MSVS 10
ADutheil
 
Posts: 368
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: Creating an array list for a Combobox

Postby Rick Lipkin » Tue Apr 03, 2012 1:27 pm

To All

As I often seem to do is make something more difficult than it actually is .. after studing the way I was creating the array .. I realized I was creating multiple elements in a single record instead of only a single element per record.

This code works just fine .. Thanks to everyone who responded to my thread!

Rick Lipkin


Code: Select all  Expand view

aTech := {}
AAdd( aTech, "          " )

If oRsEmp:eof
Else
   oRsEmp:MoveFirst()
   Do While .not. oRsEmp:eof

      If oRsEmp:Fields("Shop Repairs"):Value  = .t. .or.;
         oRsEmp:Fields("Service Calls"):Value = .t.
      Else
         oRsEmp:MoveNext()
         Loop
      Endif

      If empty( oRsEmp:Fields("First Name"):Value )
         cFname := "Unknown"
      Else
         cFname := alltrim(oRsEmp:Fields("First Name"):Value)
      Endif
      If empty( oRsEmp:Fields("Last Name"):Value )
         cLname := "Unknown"
      Else
         cLname := alltrim(oRsEmp:Fields("Last Name"):Value)
      Endif

      cName := cFname+" "+cLname

      AAdd( aTech, cName )

      oRsEmp:MoveNext()

   End Do
Endif

oRsEmp:CLose()

..
..

REDEFINE COMBOBOX oTech  var cTech   ID 127 of oWorkB  ;
               ITEMS aTech

 
User avatar
Rick Lipkin
 
Posts: 2665
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 115 guests