Private Sub Command1_Click() Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Dim i As Integer Dim intCount As Integer Dim intEndRow As Integer Dim intStart As Integer cn.CursorLocation = adUseClient cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\mydb.mdb;Persist Security Info=False"
rs.Open "select * from mytable", cn, adOpenStatic, adLockOptimistic
If rs.RecordCount > 0 Then While Not rs.EOF For i = rs.Fields.Count - 1 To 0 Step -1 If Len(rs.Fields(2)) > intCount Then intCount = Len(rs.Fields(2)) End If Next i rs.MoveNext Wend
rs.MoveFirst End If
' intCount = intCount + 1
With MSFlexGrid1 .Col = Text2.Text 'Text2指定要填入的列
For i = .Rows - 1 To 1 Step -1 .Row = i
If Trim(.Text) <> "" Then intEndRow = i Exit For End If Next i
If intEndRow > 0 Then intStart = fun_get_location(Text2.Text, 1, intEndRow) While intStart > 0 For m = intStart To intEndRow .Row = m If rs.EOF Then rs.AddNew rs.Fields(0) = m - intStart + 1 rs.Fields(2) = Space(intCount) & .Text rs.Update Else rs.Fields(2) = rs.Fields(2) & Space(intCount - Len(rs.Fields(2))) & .Text rs.Update End If
rs.MoveNext Next m
rs.MoveFirst intCount = intCount + 1 intStart = fun_get_location(Text2.Text, intStart, intEndRow) Wend End If End With
rs.Close cn.Close Set rs = Nothing Set cn = NothingEnd SubPrivate Function fun_get_location(ByVal intCol As Integer, ByVal intStart As Integer, ByVal intEnd As Integer) As Integer Dim i As Integer Dim strTemp As String
For i = intStart To intEnd strTemp = strTemp & MSFlexGrid1.TextMatrix(i, intCol) Next i
If InStr(1, strTemp, Text1.Text) > 0 Then fun_get_location = intStart + InStr(1, strTemp, Text1.Text) + Len(Text1.Text) - 1 Else fun_get_location = 0 End If End FunctionPrivate Sub Form_Load() Dim TextLine Dim strFileName As String Dim a() As String Dim i As Integer Dim j As Integer With MSFlexGrid1 .Rows = 20 .Cols = 6 strFileName = App.Path & "\testdata2.txt" Open strFileName For Input As #1 ' 打开文件。 Do While Not EOF(1) ' 循环至文件尾。 Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。 i = i + 1 .Row = i For j = 1 To Len(TextLine) .Col = j .Text = Mid(TextLine, j, 1) Next j Loop Close #1 ' 关闭文件。 End WithEnd Sub
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim i As Integer
Dim intCount As Integer
Dim intEndRow As Integer
Dim intStart As Integer cn.CursorLocation = adUseClient
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\mydb.mdb;Persist Security Info=False"
rs.Open "select * from mytable", cn, adOpenStatic, adLockOptimistic
If rs.RecordCount > 0 Then
While Not rs.EOF
For i = rs.Fields.Count - 1 To 0 Step -1
If Len(rs.Fields(2)) > intCount Then
intCount = Len(rs.Fields(2))
End If
Next i
rs.MoveNext
Wend
rs.MoveFirst
End If
' intCount = intCount + 1
With MSFlexGrid1
.Col = Text2.Text 'Text2指定要填入的列
For i = .Rows - 1 To 1 Step -1
.Row = i
If Trim(.Text) <> "" Then
intEndRow = i
Exit For
End If
Next i
If intEndRow > 0 Then
intStart = fun_get_location(Text2.Text, 1, intEndRow)
While intStart > 0
For m = intStart To intEndRow
.Row = m
If rs.EOF Then
rs.AddNew
rs.Fields(0) = m - intStart + 1
rs.Fields(2) = Space(intCount) & .Text
rs.Update
Else
rs.Fields(2) = rs.Fields(2) & Space(intCount - Len(rs.Fields(2))) & .Text
rs.Update
End If
rs.MoveNext
Next m
rs.MoveFirst
intCount = intCount + 1
intStart = fun_get_location(Text2.Text, intStart, intEndRow)
Wend
End If
End With
rs.Close
cn.Close
Set rs = Nothing
Set cn = NothingEnd SubPrivate Function fun_get_location(ByVal intCol As Integer, ByVal intStart As Integer, ByVal intEnd As Integer) As Integer
Dim i As Integer
Dim strTemp As String
For i = intStart To intEnd
strTemp = strTemp & MSFlexGrid1.TextMatrix(i, intCol)
Next i
If InStr(1, strTemp, Text1.Text) > 0 Then
fun_get_location = intStart + InStr(1, strTemp, Text1.Text) + Len(Text1.Text) - 1
Else
fun_get_location = 0
End If
End FunctionPrivate Sub Form_Load()
Dim TextLine
Dim strFileName As String
Dim a() As String
Dim i As Integer
Dim j As Integer
With MSFlexGrid1
.Rows = 20
.Cols = 6 strFileName = App.Path & "\testdata2.txt" Open strFileName For Input As #1 ' 打开文件。 Do While Not EOF(1) ' 循环至文件尾。
Line Input #1, TextLine ' 读入一行数据并将其赋予某变量。
i = i + 1
.Row = i
For j = 1 To Len(TextLine)
.Col = j
.Text = Mid(TextLine, j, 1)
Next j Loop Close #1 ' 关闭文件。
End WithEnd Sub
ADO的ADDNEW?
SQL的"INSERT"?