1

ここからどこに向かうべきかよくわかりません。テーブルをループし、すべてのフィールド名を文字列に書き込んで、コンボ ボックスの値の行ソースとして使用する小さなコードがあります。これらの項目をアルファベット順に並べたいのですが、文字列変数 (またはコンボ ボックスの RowSource プロパティ) を使用してこれを行う最善の方法がよくわかりません。

これを行う最善の方法についての考えや提案はありますか?

ここで役立つ場合は、私が持っているコードです:

Dim strFields As String
Dim fldTemp As Field
Dim intCount As Integer
Dim setData As DAO.Recordset

Set setData = CurrentDb.OpenRecordset("SELECT * FROM tblEnvironment WHERE 1 = 2")

For Each fldTemp In setData.Fields
    strFields = strFields & ", " & fldTemp.Name
Next

strFields = Mid(strFields, 3)

For intCount = 1 To 10
    Controls("cboField" & intCount).RowSource = strFields
Next

StrFields は、私がアルファベット化したいものです。前もって感謝します!

4

2 に答える 2

3

作成している文字列を配列に変換し、その配列を並べ替えます。Join次に、配列をコンマ区切りの文字列に変換するために使用できます

ここにあるバブルソートを使用して、これがあなたのコードを調整するものです

Dim strFields As String
Dim fldTemp As Field
Dim intCount As Integer
Dim setData As DAO.Recordset
Dim FieldList() As String ' array to hold field names

Set setData = CurrentDb.OpenRecordset("SELECT * FROM tblEnvironment WHERE 1 = 2")

intCount = 0
For Each fldTemp In setData.Fields
    ReDim Preserve FieldList(intCount + 1) ' expand the array for each field name
    FieldList(intCount) = fldTemp.Name
    intCount = intCount + 1
Next

BubbleSort FieldList 'sort the fieldnames

strFields = Join(FieldList, ",") 'join the names together with commas
strFields = Mid(strFields, 3)

For intCount = 1 To 10
    Controls("cboField" & intCount).RowSource = strFields
Next

リンクの腐敗の場合に備えて、バブルソートコード:

Sub BubbleSort(arr)
  Dim strTemp As String
  Dim i As Long
  Dim j As Long
  Dim lngMin As Long
  Dim lngMax As Long
  lngMin = LBound(arr)
  lngMax = UBound(arr)
  For i = lngMin To lngMax - 1
    For j = i + 1 To lngMax
      If arr(i) > arr(j) Then
        strTemp = arr(i)
        arr(i) = arr(j)
        arr(j) = strTemp
      End If
    Next j
  Next i
End Sub
于 2013-08-21T14:43:51.847 に答える
1

から文字列配列を作成するとstrFields、 を使用WizHook.SortStringArrayして配列を並べ替えることができます。SortStringArray下にデモ手順を追加しました。

SortStringArrayこのようなコードで使用できます...

Dim astrFields() As String
astrFields = Split(strFields, ", ")
WizHook.SortStringArray astrFields

もう一度文字列として必要な場合は...

strFields = Join(astrFields, ", ")

または、アレイを歩いて通り抜けることができるかもしれません...

For intCount = 0 To UBound(astrFields)
    Debug.Print intCount, astrFields(intCount)
Next

についての情報WizHookは怖いです。謎SortStringArrayを探っていたときに、このメソッドのサンプル プロシージャを作成しました。WizHook

Public Sub WizHook_SortStringArray()
   ' The WizHook Key is not required for this procedure.
   'WizHook.Key = 51488399
    Dim a(3) As String
    Dim i As Long
    a(0) = "zulu"
    a(1) = "alpha"
    a(2) = "gamma"
    a(3) = "delta"
    WizHook.SortStringArray a
    For i = 0 To 3
        Debug.Print a(i)
    Next i
End Sub

その手順の元のバージョンからのコメントを追加しました。一部のWizHookメソッドにはKey値が必要です。しかし、私は信じてSortStringArrayいませんでした。書かれているとおりに動作しない場合は、WizHook.Key回線を有効にしてみてください。

于 2013-08-21T15:05:02.290 に答える