Mediante la siguiente función se añade el valor “(todos)” a un cuadro combinado o un cuadro de lista. Tras crear el elemento que contiene los datos que queremos mostrar hay que cambiar la propiedad “RowSourceType del valor “Table/Quert” a “AddAllToList”, y establece el valor de la propiedad Tag del cuadro combinado o del cuadro de lista el número de la comna en la que se quiere que aparezca el valor “(todos)”.

La función también permite que tras el número de la columna indicado en la propiedad Tag se puede añadir un punto y coma (;) y el texto que quieras que aparezca y que sea distinto de “(todos)” (por ejemplo: valor de Tag=”2;<ninguno>”. Muestra el valor “ninguno” en la segunda columna de la lista).

El ejemplo muestra también como crear una consulta UNION , se puede utilizar la función o la consulta de unión para agregar (”todos”) como la primera fila de cualquier control existente de cuadro de cuadro combinado o lista de cuadro combinado en una aplicación de Microsoft Access.

Function AddAllToList (C As Control, ID As Long, Row As Long, Col As Long, Code As Integer) As Variant
Static DB As Database, RS As Recordset
Static DISPLAYID As Long
Static DISPLAYCOL As Integer
Static DISPLAYTEXT As String
Dim Semicolon As Integer
On Error GoTo Err_AddAllToList
Select Case Code
Case LB_INITIALIZE
‘Comprueba si la función se está utilizando.
If DISPLAYID <> 0 Then
MsgBox “AddAllToList está sienda utilizado con otro control!”
AddAllToList = False
Exit Function
End If

‘ Parse the display column and display text from the Tag property.
DISPLAYCOL = 1
DISPLAYTEXT = “(All)”
If Not IsNull(C.Tag) Then
SemiColon = InStr(C.Tag, “;”)
If SemiColon = 0 Then
DISPLAYCOL = Val(C.Tag)
Else
DISPLAYCOL = Val(Left(C.Tag, SemiColon – 1))
DISPLAYTEXT = Mid(C.Tag, SemiColon + 1)
End If
End If

‘ Open the recordset defined in the RowSource property.
Set DB = DBEngine.Workspaces(0).Databases(0)
Set RS = DB.OpenRecordset(C.RowSource, DB_OPEN_SNAPSHOT)

‘ Record and return the ID for this function.
DISPLAYID = Timer
AddAllToList = DISPLAYID

Case LB_OPEN
AddAllToList = DISPLAYID

Case LB_GETROWCOUNT
‘ Return the number of rows in the recordset.
RS.MoveLast
AddAllToList = RS.RecordCount + 1

Case LB_GETCOLUMNCOUNT
‘ Return the number of fields (columns) in the recordset.
AddAllToList = RS.Fields.Count

Case LB_GETCOLUMNWIDTH
AddAllToList = -1

Case LB_GETVALUE
‘ Are you requesting the first row?
If Row = 0 Then
‘ Should the column display “(All)”?
If Col = DISPLAYCOL – 1 Then
‘ If so, return “(All).”
AddAllToList = DISPLAYTEXT
Else
‘ Otherwise, return NULL.
AddAllToList = Null
End If
Else
‘ Grab the record and field for the specified row/column.

RS.MoveFirst
RS.Move Row – 1
AddAllToList = RS(Col)
End If
Case LB_END
DISPLAYID = 0
RS.Close
End Select

Bye_AddAllToList:
Exit Function

Err_AddAllToList:
Beep: MsgBox Error$, 16, “AddAllToList”
AddAllToList = False
Resume Bye_AddAllToList
End Function

Fuente: www.support.microsoft.com

Leave a Reply