列表選項包含FirstName、 LastName、 Company 和 City。為了正確地安排第一欄的格式,我們必須發現字節數最多的名字''Nathan'',然后為全部7個字符增加一個無間斷空間。然后我們給John和Sue增加適當數量的空格,使它成為7個字符,在結尾增加管道符號,現在我們就有了一個格式化了的欄。
要注意我們在以上代碼中增加了< FONT > 標記。這是為了在Netscape上使用考慮,因為Netscape不象 IE能夠識別字體屬性。還要注意< FORM > 標記的出現。這也是Netscape 的要求。IE允許你繪制表單域、 選項列表等,不用將它們放在< FORM > 標記中,而在Netscape ,如果想要提取表單控制的話,就要求 表單的標記。
對于我們這個特定情況,你可能想要在函數的最開頭包含一個On Error Resume Next 語句。把這個語句放在代碼中允許代碼在發生錯誤的情況下繼續執行(也許在返回的記錄集中,代碼在某一行中遇到了一個意外的值)。代碼中有這個語句,你還可以建立選項列表的內容。缺點是在發生錯誤的情況,你的應用程序用戶將不能直接向你報告錯誤,也許這個錯誤會導致下一個錯誤,使你的調試工作更加困難。
''Loop for formatting the select list values For i = 0 to UBound(column) maxStrLenTemp = 0 finalMaxLenArray(i) = 0 '' Loop through recordset to determine the maximum length string '' in given column If Not rsSelect.BOF Then rsSelect.MoveFirst Do While Not rsSelect.EOF maxStrLenTemp = Len(Trim(rsSelect(CStr(Trim(column(i)))))) If Not IsNull(maxStrLenTemp) Then If CInt(maxStrLenTemp) > CInt(finalMaxLenArray(i)) Then finalMaxLenArray(i) = maxStrLenTemp End If rsSelect.MoveNext Loop
'' If the derived col length is greater than the maxcollen parameter '' then reset col length. If IsArray(maxcollen) = True Then If CInt(maxcollen(i)) < > -1 Then If finalMaxLenArray(i) > CInt(maxcollen(i)) Then finalMaxLenArray(i) = CInt(maxcollen(i)) End If Else If CInt(maxcollen) < > -1 Then If finalMaxLenArray(i) > CInt(maxcollen) Then finalMaxLenArray(i) = CInt(maxcollen) End If End If Next
''strDif Can be negative number if maxcollen is in effect. strDif = CInt(finalMaxLenArray(n)) - Len(descTemp) If strDif < 0 Then descTemp = Mid(descTemp, 1, finalMaxLenArray(n) + 1) Else For x = 0 to strDif ''0 for one extra descTemp = descTemp & " " Next End If
然后我們在字符串結尾處增加"|" 字符,然后將那個字符串增加到最后的字符串finalDesc上。 注意循環的次數要與選項列表中的欄數相當,這樣結束時就可以有一個完整的選項列表行。 If n < > UBound(column) Then descTemp = descTemp & "|" strDif = 0