Some skilled used in Excel Handling~
Private Sub LoadDataTypeField()
'Set DB connection to MGISDBSRV
Dim DBcfg As DBconfig
Dim strSQL As String
Dim iRowscount, iTableRowCount, iTotalCount, iArrayCount As Integer
Dim i, j As Integer
Dim cm, cf, cB, cI As String 'cm is Cell Datatype "G" , cf is Cell FieldName "E", cb is the Cell Table Name "B"
' cI is cell isNull type "I"
With DBcfg
.strPassword = txtPwd.Text
.strServerName = txtServerName.Text
.strUserName = txtPwd.Text
.strSDEInstance = "sde_gisdbw"
.strSIDName = txtSID.Text
.strSDEOwner = txtSDEOwner.Text
End With
Set mobjConn = New ADODB.Connection
mstrConn = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;Data Source=" & DBcfg.strSIDName & ";User ID =" & DBcfg.strUserName & ";Password=" & DBcfg.strPassword
mobjConn.Open mstrConn
'iRowsCount = Sheet1.Cells(Sheet1.Rows.Count, "G").End(xlUp).Row
iRowscount = ActiveSheet.UsedRange.Rows.Count
iTotalCount = 1
ProgressBar1.Visible = True
ProgressBar1.Max = TB_COUNT
Set objRs = New ADODB.Recordset
For i = 1 To TB_COUNT
strSQL = "select * from user_tab_columns where table_name='" & Trim(TBNameList(i)) & "'"
objRs.Open strSQL, mobjConn, adOpenStatic, adLockOptimistic
iTableRowCount = objRs.RecordCount
If iTableRowCount <> 0 Then
'if the Table has field , then record them
' cB = "B" & Trim(Str(iTotalCount)) 'Table Name
' Range(cB).Select
' ActiveCell.FormulaR1C1 = TBNameList(i)
objRs.MoveFirst
Debug.Print "Field counts of " & TBNameList(i) & "are: " & Str(iTableRowCount)
ReDim DataTypeList(1 To TB_COUNT, 1 To iTableRowCount)
For j = 1 To iTableRowCount 'Start Inner Loop
cm = "G" & Trim(Str(iTotalCount)) 'Data Type and data length
cf = "E" & Trim(Str(iTotalCount)) 'Field Name
cB = "B" & Trim(Str(iTotalCount)) 'Table Name
cI = "I" & Trim(Str(iTotalCount)) 'Table Name
DataTypeList(i, j).TableName = TBNameList(i)
If objRs.Fields("DATA_TYPE").Value = "NUMBER" Then ' Only number type has data scale
'if the data Precision is null,then replace it with zero
If IsNull(objRs.Fields("DATA_PRECISION").Value) = True Then
DataTypeList(i, j).XLSDataTypeColumn = objRs.Fields("DATA_TYPE").Value _
& "(" & objRs.Fields("DATA_LENGTH").Value & "," & "0" & ")"
DataTypeList(i, j).FieldName = objRs.Fields("COLUMN_NAME").Value
DataTypeList(i, j).IsNullField = objRs.Fields("NULLABLE").Value
'if the data precision is not null, then replace it with DATA_TYPE+DATA_PRECISION
Else
DataTypeList(i, j).XLSDataTypeColumn = objRs.Fields("DATA_TYPE").Value _
& "(" & objRs.Fields("DATA_LENGTH").Value & "," & objRs.Fields("DATA_PRECISION").Value & ")"
DataTypeList(i, j).FieldName = objRs.Fields("COLUMN_NAME").Value
DataTypeList(i, j).IsNullField = objRs.Fields("NULLABLE").Value
End If
Else ' if the data type is not number, no data precision needed
DataTypeList(i, j).XLSDataTypeColumn = objRs.Fields("DATA_TYPE").Value _
& "(" & objRs.Fields("DATA_LENGTH").Value & ")"
DataTypeList(i, j).FieldName = objRs.Fields("COLUMN_NAME").Value
DataTypeList(i, j).IsNullField = objRs.Fields("NULLABLE").Value
End If
Range(cB).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).TableName
Range(cm).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).XLSDataTypeColumn
Range(cf).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).FieldName
Range(cI).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).IsNullField
objRs.MoveNext
iTotalCount = iTotalCount + 1
Debug.Print DataTypeList(i, j).TableName & "*" & DataTypeList(i, j).FieldName & "*" & DataTypeList(i, j).XLSDataTypeColumn
Next 'End Inner Loop
objRs.Close
Else
'if the Table has no fields, then redim the Array to DataTypeList(1 to 440,1 to 1)
'and set the XLSDataTypeColumn to empty
ReDim DataTypeList(1 To TB_COUNT, 1 To 1)
DataTypeList(i, 1).XLSDataTypeColumn = ""
objRs.Close
End If
ProgressBar1.Value = i
Next
ProgressBar1.Visible = False
Set objRs = Nothing
Set mobjConn = Nothing
End Sub
'Set DB connection to MGISDBSRV
Dim DBcfg As DBconfig
Dim strSQL As String
Dim iRowscount, iTableRowCount, iTotalCount, iArrayCount As Integer
Dim i, j As Integer
Dim cm, cf, cB, cI As String 'cm is Cell Datatype "G" , cf is Cell FieldName "E", cb is the Cell Table Name "B"
' cI is cell isNull type "I"
With DBcfg
.strPassword = txtPwd.Text
.strServerName = txtServerName.Text
.strUserName = txtPwd.Text
.strSDEInstance = "sde_gisdbw"
.strSIDName = txtSID.Text
.strSDEOwner = txtSDEOwner.Text
End With
Set mobjConn = New ADODB.Connection
mstrConn = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;Data Source=" & DBcfg.strSIDName & ";User ID =" & DBcfg.strUserName & ";Password=" & DBcfg.strPassword
mobjConn.Open mstrConn
'iRowsCount = Sheet1.Cells(Sheet1.Rows.Count, "G").End(xlUp).Row
iRowscount = ActiveSheet.UsedRange.Rows.Count
iTotalCount = 1
ProgressBar1.Visible = True
ProgressBar1.Max = TB_COUNT
Set objRs = New ADODB.Recordset
For i = 1 To TB_COUNT
strSQL = "select * from user_tab_columns where table_name='" & Trim(TBNameList(i)) & "'"
objRs.Open strSQL, mobjConn, adOpenStatic, adLockOptimistic
iTableRowCount = objRs.RecordCount
If iTableRowCount <> 0 Then
'if the Table has field , then record them
' cB = "B" & Trim(Str(iTotalCount)) 'Table Name
' Range(cB).Select
' ActiveCell.FormulaR1C1 = TBNameList(i)
objRs.MoveFirst
Debug.Print "Field counts of " & TBNameList(i) & "are: " & Str(iTableRowCount)
ReDim DataTypeList(1 To TB_COUNT, 1 To iTableRowCount)
For j = 1 To iTableRowCount 'Start Inner Loop
cm = "G" & Trim(Str(iTotalCount)) 'Data Type and data length
cf = "E" & Trim(Str(iTotalCount)) 'Field Name
cB = "B" & Trim(Str(iTotalCount)) 'Table Name
cI = "I" & Trim(Str(iTotalCount)) 'Table Name
DataTypeList(i, j).TableName = TBNameList(i)
If objRs.Fields("DATA_TYPE").Value = "NUMBER" Then ' Only number type has data scale
'if the data Precision is null,then replace it with zero
If IsNull(objRs.Fields("DATA_PRECISION").Value) = True Then
DataTypeList(i, j).XLSDataTypeColumn = objRs.Fields("DATA_TYPE").Value _
& "(" & objRs.Fields("DATA_LENGTH").Value & "," & "0" & ")"
DataTypeList(i, j).FieldName = objRs.Fields("COLUMN_NAME").Value
DataTypeList(i, j).IsNullField = objRs.Fields("NULLABLE").Value
'if the data precision is not null, then replace it with DATA_TYPE+DATA_PRECISION
Else
DataTypeList(i, j).XLSDataTypeColumn = objRs.Fields("DATA_TYPE").Value _
& "(" & objRs.Fields("DATA_LENGTH").Value & "," & objRs.Fields("DATA_PRECISION").Value & ")"
DataTypeList(i, j).FieldName = objRs.Fields("COLUMN_NAME").Value
DataTypeList(i, j).IsNullField = objRs.Fields("NULLABLE").Value
End If
Else ' if the data type is not number, no data precision needed
DataTypeList(i, j).XLSDataTypeColumn = objRs.Fields("DATA_TYPE").Value _
& "(" & objRs.Fields("DATA_LENGTH").Value & ")"
DataTypeList(i, j).FieldName = objRs.Fields("COLUMN_NAME").Value
DataTypeList(i, j).IsNullField = objRs.Fields("NULLABLE").Value
End If
Range(cB).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).TableName
Range(cm).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).XLSDataTypeColumn
Range(cf).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).FieldName
Range(cI).Select
ActiveCell.FormulaR1C1 = DataTypeList(i, j).IsNullField
objRs.MoveNext
iTotalCount = iTotalCount + 1
Debug.Print DataTypeList(i, j).TableName & "*" & DataTypeList(i, j).FieldName & "*" & DataTypeList(i, j).XLSDataTypeColumn
Next 'End Inner Loop
objRs.Close
Else
'if the Table has no fields, then redim the Array to DataTypeList(1 to 440,1 to 1)
'and set the XLSDataTypeColumn to empty
ReDim DataTypeList(1 To TB_COUNT, 1 To 1)
DataTypeList(i, 1).XLSDataTypeColumn = ""
objRs.Close
End If
ProgressBar1.Value = i
Next
ProgressBar1.Visible = False
Set objRs = Nothing
Set mobjConn = Nothing
End Sub