VB学习之 按钮响应

Sub 按钮5_Click() Dim strPath As String '输出SQL文件的路径 Dim strFile As String '输出SQL文件的路径+文件名 Dim strPrint As String '输出内容

Dim i, j As Integer         '循环变量

Dim strTable As String      '表名
Dim strField As String      '字段名
Dim strFieldN As String      '字段名(汉字)
Dim strType As String       '字段类型
Dim strLength As String     '字段长度
Dim strNull As String       '非空
Dim strPrimaryKey As String '是否为主键
Dim strTmpPK As String      '是否为主键
'Dim strRemarks As String    '备注
Dim bFirst As Boolean


strPath = "E:\"
'从第个工作表开始循环

    strPrimaryKey = ""

    
    'If strTable = "" Then Exit Sub
    strFile = strPath & "database.sql"
     '以输出的格式打开文件strFileName
    Open strFile For Output As #1
    For i = 4 To Worksheets.Count
     '获得表名
    strTable = Worksheets(i).Cells(4, 3).Value
    strPrint = "USE CRMIS "
    strPrint = strPrint & vbNewLine & "CREATE TABLE " & strTable & "(" & vbNewLine
    '从第三开始循环至使用行
    bFirst = True
    
    For j = 6 To Worksheets(i).UsedRange.Rows.Count
        strLength = ""
        strNull = ""
        strDefault = ""
        strTmpPK = ""
    
        '字段名
        strField = Worksheets(i).Cells(j, 2).Value
        If strField = "" Then Exit For
        strFieldN = Worksheets(i).Cells(j, 6).Value
        '字段类型
        strType = Worksheets(i).Cells(j, 3).Value
        '长度
        strLength = Worksheets(i).Cells(j, 4).Value
        '非空
        strNull = Worksheets(i).Cells(j, 5).Value
        '主键
        strTmpPK = Worksheets(i).Cells(j, 7).Value
        If strTmpPK <> "" Then strPrimaryKey = "," & Worksheets(i).Cells(j, 2).Value
        '备注
      '  strRemarks = Worksheets(i).Cells(j, 8).Value

strPrint = strPrint & vbTab & strField

        Select Case strType
        Case "INTEGER"
        strType = "bigint"
        'strPrint = strPrint & " " & strType
        Case "NUMBER"
        strType = "decimal"
        'strPrint = strPrint & " " & strType
        Case "DATE"
        strType = "datetime"
        'strPrint = strPrint & " " & strType
        Case "NCBOLB"
        strType = "text"
        'strPrint = strPrint & " " & strType
        Case "VARCHAR2"
        strType = "varchar"
        'strPrint = strPrint & " " & strType
        Case "NVARCHAR2"
        strType = "varchar"
        Case "FLOAT"
        strType = "float"
        Case Else
        strType = "text"
        End Select
          
          If bFirst = True Then
        
       strPrint = strPrint & " " & strType & " IDENTITY(1,1)"
        bFirst = False
        Else
       strPrint = strPrint & " " & strType
        End If
        
       ' strPrint = strPrint & " " & strType
        
        If strLength <> "" And strLength <> "-" Then strPrint = strPrint & "(" & strLength & ")"
        If strNull <> "" Then strPrint = strPrint & " " & "NOT NULL"
       ' If strRemarks <> "" Then strPrint = strPrint & " " & strRemarks
        'strPrint = strPrint & " COMMENT '" & strFieldN & "'"
        strPrint = strPrint & "," & vbNewLine
    Next
    
    If strPrimaryKey <> "" Then
        strPrimaryKey = Right(strPrimaryKey, Len(strPrimaryKey) - 1)
        strPrint = strPrint & vbNewLine & vbTab & "PRIMARY KEY(" & strPrimaryKey & ")"
    Else
        strPrint = Left(strPrint, Len(strPrint))
    End If
    
    strPrint = strPrint & vbNewLine & ");"
    
    Print #1, strPrint
    Next
    Close #1

End Sub

转载于:https://my.oschina.net/yangxiaoguang/blog/84892

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值