English 中文(简体)
ADO OpenSchema的使用 表格清单、栏目清单和数值的方法
原标题:Use the ADO OpenSchema Method to get table list, column list and value
  • 时间:2010-07-26 10:48:15
  •  标签:
  • vb6

我无法从价值表箱中获取选定栏的数值。 我错了。 私人名单清单中似乎有一些错误。

Option Explicit

  The database file name.
 Private m_DBFile As String

  List the fields in this table.
 Private Sub ListFields(ByVal db_file As String, ByVal db_table_name As String)
 Dim statement As String
 Dim conn As ADODB.Connection
 Dim rs As ADODB.Recordset

  Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & db_file & ";" & _
    "Persist Security Info=False"
conn.Open

lstFields.Clear

  Use OpenSchema and get the table names.
Set rs = conn.OpenSchema(adSchemaColumns, _
    Array(Empty, Empty, db_table_name))

Do While Not rs.EOF
    lstFields.AddItem rs!column_name
    rs.MoveNext
Loop

rs.Close
conn.Close
End Sub

  List the tables in the database.
Private Sub ListTables(ByVal db_name As String)
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset

  Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & db_name & ";" & _
    "Persist Security Info=False"
conn.Open

lstTables.Clear
lstFields.Clear
lstValues.Clear

  Use OpenSchema and get the table names.
Set rs = conn.OpenSchema(adSchemaTables, _
    Array(Empty, Empty, Empty, "Table"))
Do While Not rs.EOF
    lstTables.AddItem rs!TABLE_NAME
    rs.MoveNext
Loop

rs.Close
conn.Close
End Sub

 Private Sub ListValues(ByVal db_file As String, ByVal db_column_name As String)
 Dim statement As String
 Dim conn As ADODB.Connection
 Dim rs As ADODB.Recordset

  Open a connection.
Set conn = New ADODB.Connection
conn.ConnectionString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & db_file & ";" & _
    "Persist Security Info=False"
conn.Open

lstValues.Clear

  Use OpenSchema and get the Column  Value.
 Set rs = conn.OpenSchema(adSchemaColumns, _
    Array(Empty, Empty, db_table_name))
Set rs = conn.OpenSchema(adSchemaIndexes, _
Array(Empty, Empty, Empty, Empty,db_column_name))


Do While rs.EOF
    lstValues.AddItem rs!INDEX_NAME
    rs.MoveNext
Loop

rs.Close
conn.Close
End Sub
Private Sub lstTables_Click()
If lstTables.ListIndex < 0 Then Exit Sub

ListFields m_DBFile, lstTables.Text
End Sub

Private Sub lstFields_Click()
Dim db_column_name As String
If lstFields.ListIndex < 0 Then Exit Sub
db_column_name = lstFields.List(lstFields.ListIndex)

ListValues m_DBFile, lstValues.Text
End Sub

Private Sub mnudbFile_Click()
 Open existing Weight database file
cdlFiles.Flags = cdlOFNFileMustExist + cdlOFNPathMustExist
cdlFiles.Filter = "Database Files (*.mdb)|*.mdb"
cdlFiles.DialogTitle = "Open Database File"
cdlFiles.InitDir = App.Path
On Error GoTo HandleErrors
ReOpen:
cdlFiles.ShowOpen

m_DBFile = cdlFiles.FileName

 List the tables.
ListTables m_DBFile
Exit Sub
HandleErrors:
If Err.Number = cdlCancel Then Exit Sub
Select Case MsgBox(Err.Description, vbCritical + vbAbortRetryIgnore, "Error Number" + Str(Err.Number) + " in " + Err.Source)
Case vbAbort
Exit Sub
Case vbRetry
Resume ReOpen
Case vbIgnore
  Resume Next
 End Select

End Sub
问题回答

名单上的人没有发言。

Do While rs.EOF 

应当:

Do While Not rs.EOF 

1. 修改贵国的《名单》:

  Private Sub ListValues(ByVal db_file As String, ByVal db_table_name as String, ByVal        
  db_column_name As String)
  Dim statement As String
  Dim conn As ADODB.Connection
  Dim rs As ADODB.Recordset

开放联系

  Set conn = New ADODB.Connection
  conn.ConnectionString = _
  "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=" & db_file & ";" & _
  "Persist Security Info=False"
  conn.Open

lstValues.Clear

Set rs = New ADODB.Recordset

rs.Open "SELECT*FROM " & db_table_name & " WHERE " & db_column_name, conn, adOpenStatic, adLockOptimistic

Do While Not rs.EOF
     lstValues.AddItem rs.Fields(db_column_name).Value
     rs.MoveNext
Loop

rs.Close
conn.Close
End Sub

您的编码中的另一个错误内容是:

Your Code:
    Set rs = conn.OpenSchema(adSchemaIndexes, _
    Array(Empty, Empty, Empty, Empty,db_column_name))

Right code:
    Set rs = conn.OpenSchema(adSchemaIndexes, _
    Array(Empty, Empty, Empty, Empty,db_table_name))




相关问题
Prevent windows from queuing shellexecute requests

Win.ShellExecute 0, "open", "C:dirprogram.exe", "arguments", vbNullString, SW_SHOWNORMAL Win.ShellExecute 0, "open", "http://www.google.com", vbNullString, vbNullString, SW_SHOWNORMAL I want google....

Why is My Loop Only Deleting One File?

Using VB6 In a folder, i have n number of files, i want to delete a 0 kb files code Dim filename5 As String filename5 = Dir$(txtsourcedatabasefile & "*_*", vbDirectory) MsgBox filename5 Do ...

How to check the filesize?

Using VB6 I have the text file with different sizes, so i want to delete the file where filesize = 0 kb. How to make a vb6 code for deleting the 0 kb files. Need vb6 code Help

File Rename problem?

I m using VB6 and I have a folder where I have n number of files. I want to change the file extension to .txt. I used the code below to change the extension of all .fin files to .txt. Dim filename1 ...

Error 20728-F while in using Crystal Reports in VB6

I m using Crystal Reports in my VB6 project, but I m facing error while loading the report in crystalreport1.action=1; Please give me some solution for this problem. It is showing the error as Error ...

DllRegisterServer entry point was not found

When running my vb6 application I am getting error like, runtime error 53 : file not found: rscomclNoMsg.dll then i tried to register that dll from cmd line using regsvr32. Then I am getting ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签