Visual Basic Tips


schema.iniの作成


CSV形式のファイルを本サイトのJetデータベース(DAO)テーブルのCSVファイルインポートを使用してMDBにインポートするとCSVファイルの内容が全てインポートされていなかったり、インポートされないフィールドがあったりと訳のわからないことになることがあります。これはインポートしようとするフィールドが文字列型の時、CSVファイルの対象となるデータ項目にダブルコーテーションがついていないためにおこります、これを回避するためにはインポートしようとするファイルが格納されているフォルダと同一フォルダにshema.ini(フィールドの型定義)を作成する必要があります。
Private Sub Command3_Click()
  
Call CrtShemaini(App.Path & "\db1.mdb", "テーブル1", App.Path, "import.csv")
End Sub

'--------------------------------------------------------------------
' Module
'--------------------------------------------------------------------
Public Sub CrtShemaini(strDbPath As String, strTblName As String, strOutPath As String, strFileName As String)
  
Dim ret As Integer
  
Dim ws As Workspace
  
Dim db As Database
  
Dim rs As Recordset
  
Dim fld As Field
  
Dim strType As String
  
Dim i As Integer
  
Dim intFileno As Integer

  intFileno = FreeFile
  
Open strOutPath & "\schema.ini" For Output As intFileno
  
Print #intFileno, "[" & strFileName & "]"
  
Print #intFileno, "ColNameHeader = True"
  
Print #intFileno, "CharacterSet = OEM"
  
Print #intFileno, "Format = CSVDelimited"

  
Set ws = DBEngine.Workspaces(0)
  
Set db = ws.OpenDatabase(strDbPath, False, True)
  
Set rs = db.OpenRecordset(strTblName, dbOpenDynaset)

  
For Each fld In rs.Fields
    i = i + 1
    strType = "Col" & i & "="
    strType = strType & fld.Name & " "
    
Select Case fld.Type
      
Case dbText
         strType = strType & "Char"
'dbText テキスト型
         strType = strType & " Width " & fld.Size
      
Case dbMemo
         strType = strType & "LongChar"
'dbMemo メモ型
         strType = strType & " Attribute 32"
      
Case dbByte
         strType = strType & "Byte"
'dbByte バイト型 (Byte)
      
Case dbInteger
         strType = strType & "Short"
'dbInteger 整数型 (Integer)
      
Case dbLong
         strType = strType & "Integer"
'dbLong 長整数型 (Long)
      
Case dbSingle
         strType = strType & "Single"
'dbSingle 単精度浮動小数点数型 (Single)
      
Case dbDouble
         strType = strType & "Float"
'dbDouble 倍精度浮動小数点数型 (Double)
      
Case dbDate
         strType = strType & "Date"
'dbDate 日付 / 時刻型
      
Case dbCurrency
         strType = strType & "Currency"
'dbCurrency 通貨型 (Currency)
      
Case dbBoolean
         strType = strType & "Bit"
'dbBoolean Yes/No 型
    
End Select
    
Print #intFileno, strType
  
Next

  rs.Close
  db.Close
  ws.Close
  
Close #intFileno
End Sub


DownLoad vbtips118.lzh 2KB (VB6.0)