我在excel文件中有一个名为“Employee_States_File”的表。此表包含两列Name和States。两列都填充了数据。省表包含诸如“NE,WA”等国家的缩写。我有另一个名为“States_File”的excel文件,其中包含一个包含两列的表:缩写和FullStateName。该表被视为查找表,用于根据缩写查找完整的状态名称。现在,我想在VB6中编写一个代码,以便当用户单击一个按钮时,excel文件“Employee_States”表中的所有缩写名称都将根据excel表的查找表更改为完整的州名“ States_File”。 是否有意义? 请帮忙,
答案 0 :(得分:0)
如果允许将它们转换为csv文件(逗号分隔的文本文件),则可以使用Jet数据库引擎并执行常规SQL连接。
我用它来设置连接。
Public Function OpenTextConnection(ByVal FileName As String) As Connection
Dim FSO As FileSystemObject
Dim DBFolder As String
Dim TS As TextStream
Set FSO = New FileSystemObject
DBFolder = FSO.GetParentFolderName(FileName)
If FSO.FileExists(FSO.BuildPath(DBFolder, "Schema.ini")) Then
FSO.DeleteFile (FSO.BuildPath(DBFolder, "Schema.ini"))
End If
Set TS = FSO.CreateTextFile(FSO.BuildPath(DBFolder, "Schema.ini"))
TS.WriteLine "[" & FSO.GetFileName(FileName) & "]"
TS.WriteLine "Format=CSVDelimited"
TS.WriteLine "ColNameHeader = True"
TS.WriteLine "MaxScanRows = 0"
TS.Close
Set OpenTextConnection = New Connection
If FSO.FolderExists(DBFolder) Then
OpenTextConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBFolder & ";Extended Properties=""text;HDR=Yes;FMT=Delimited;"";"
Else
MsgBox DBFolder & " Does not exists.", vbExclamation
End If
End Function
每个文件都是连接中的表,您可以执行SQL连接。请注意,该示例只是一个简单的打开表。您可以使用任何有效的SQL语法。
Dim DB1 As Connection
Dim TB As Recordset
Dim FSO As FileSystemObject
Dim tImport As New DBImportList
Set FSO = New FileSystemObject
Set tImport = New DBImportList
If FSO.FileExists(FileName) Then
Set DB1 = OpenTextConnection(FileName)
Set TB = New Recordset
TB.Open "SELECT * FROM [" & FSO.GetFile(FileName).Name & "]", DB1, adOpenKeyset, adLockOptimistic, adCmdText
End If