DAOやADOのデータベース接続で失敗した時に作成しようとしたDatabaseやRecordsetの
クローズがされているかチェックしてクローズしたいんだ。
どうやったらできるか教えてくれ!
▼回答
下記にそのサンプルを示しておくから参考にしてくれ!!
Dim db As Database
Dim rs As Recordset
If (Not rs Is Nothing) Then rs.Close
If (Not db Is Nothing) Then db.Close
Dim db As Database
Dim rs As Recordset
If (Not rs Is Nothing) Then rs.Close
If (Not db Is Nothing) Then db.Close
If (Worksheets(シート名).QueryTables.Count <= 0) Then
With Worksheets(シート名).QueryTables.Add( _
Connection:="ODBC;DSN=データソース;UID=ユーザ;PWD=パスワード", _
Destination:=Worksheets(シート名).Range("A1"))
.Sql = Array("SELECT * FROM " & tableName)
.Refresh BackgroundQuery:=False
End With
else
Selection.QueryTable.Sql = Array("SELECT * FROM " & tableName)
Selection.QueryTable.Refresh BackgroundQuery:=False
End If
maxRow = Worksheets(シート名).Range("A65536").End(xlUp).row
maxCol = Worksheets(シート名).Rows(1).End(xlToRight).Column
desCol = maxCol + 1
For Each objQuery In Worksheets(シート名).QueryTables
objQuery.Delete
Next
With Worksheets(シート名).QueryTables.Add( _
Connection:="ODBC;DSN=データソース名;UID=ユーザー;PWD=パスワード", _
Destination:=Worksheets(シート名).Cells(1, desCol))
.Sql = "DELETE FROM テーブル名 WHERE 条件)
.Refresh
Do While .Refreshing
DoEvents
Loop
.Parent.Names(.Name).Delete
.Delete
End With
Worksheets(シート名).Cells(1, desCol).ClearContents
For i = 2 To maxRow
strValue = ""
For j = 1 To maxCol
If (strValue <> "") Then strValue = strValue & ", "
strValue = strValue & "'" & Worksheets(シート名).Cells(i, j).Value & "'"
Next j
With Worksheets(シート名).QueryTables.Add( _
Connection:="ODBC;DSN=データソース名;UID=ユーザー;PWD=パスワード", _
Destination:=Worksheets(シート名).Cells(1, desCol))
.Sql = "INSERT INTO テーブル名 VALUES(" & strValue & ")"
.Refresh
Do While .Refreshing
DoEvents
Loop
.Parent.Names(.Name).Delete
.Delete
End With
Worksheets(シート名).Cells(1, desCol).ClearContents
Next i
Do While .Refreshing
DoEvents
Loop
SELECT COUNT(*) FROM テーブル名 WHERE フィールド LIKE '!"#$%&()=~{`+*}_?><\%'
strSQL = "SELECT COUNT(*) FROM " & strTBL & " WHERE フィールド名= 値"
Set rs = cn.Execute(strSQL)
count = rs.Fields(0)
rs.Close
If (count = 1) Then
'更新
strSQL = "SELECT * FROM " & strTBL & " WHERE フィールド名= 値"
rs.Open strSQL, cn, adOpenForwardOnly, adLockOptimistic 'SQL文を実行
rs!フィールド名= 値
rs.Update '更新(保存)
rs.Close
Else
'新規作成
strSQL = "SELECT * FROM " & strTBL
rs.Open strSQL, cn, adOpenKeyset, adLockOptimistic 'テーブルを開く
rs.AddNew '新規レコードを追加する
rs!フィールド名= 値
rs.Update '更新(保存)
rs.Close
End If
Sub HelloWorldExampleCalc()
Dim myCalc As Object, mySheet As Object, myCell As Object
myCalc = StarDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, dummyArray)
mySheet = myCalc.getCurrentController().getActiveSheet()
myCell = mySheet.getCellByPosition(0, 1)
myCell.String = OOoMess111
myCell = mySheet.getCellByPosition(0, 2)
myCell.Value = 100
myCell.CharColor = OOoRGB(0, 200, 0)
myCell = mySheet.getCellByPosition(0, 3)
myCell.Formula = "=A3*2"
myCell.CharWeight = OOOawtFontWeightBOLD
mySheet.getCellByPosition(0, 4).CellBackColor() = OOoRGB(0, 200, 0)
Dim oBorderLine As Object = CreateUnoStruct("com.sun.star.table.BorderLine")
Dim oTableBorder As Object = CreateUnoStruct("com.sun.star.table.TableBorder")
With oBorderLine
.Color = RGB(0, 0, 0)
.OuterLineWidth = 1
End With
With oTableBorder
.IsLeftLineValid = True
.IsTopLineValid = True
.IsRightLineValid = True
.IsBottomLineValid = True
.LeftLine = oBorderLine
.TopLine = oBorderLine
.RightLine = oBorderLine
.BottomLine = oBorderLine
End With
mySheet.getCellRangeByName("A2:B3").TableBorder() = oTableBorder
MsgBox(OOoMess105)
myCalc.close(True)
End Sub
sub Main
Dim dbg As object
dbg = CreateUnoService("com.sun.star.sheet.SpreadsheetDocument")
MsgBox(dbg.Dbg_SupportedInterfaces)
MsgBox(dbg.Dbg_Properties)
MsgBox(dbg.Dbg_Methods)
end sub
If (Me.DataGrid1.VisibleRowCount > 0) Then
Me.DataGrid1.PreferredRowHeight = _
GetRowHeight(i, Me.DataGrid1.PreferredRowHeight, Me.DataGrid1)
End If
Private Sub SetRowHeight()
'全行数分ループする
For i As Integer = 0 To Me.DataGrid1.VisibleRowCount - 1
If (Me.DataGrid1.PreferredRowHeight <> _
GetRowHeight(i, Me.DataGrid1.PreferredRowHeight, Me.DataGrid1)) Then
SetRowHeight(i, Me.DataGrid1.PreferredRowHeight, Me.DataGrid1)
End If
Next i
End Sub
Public Function GetRowHeight(ByVal Row As Integer, ByVal height As Integer, ByVal oDG As DataGrid) As Integer
Dim p As Reflection.PropertyInfo = _
GetType(DataGrid).GetProperty("DataGridRows", _
Reflection.BindingFlags.FlattenHierarchy Or _
Reflection.BindingFlags.IgnoreCase Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic Or _
Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.Static)
Dim obj As Object = _
CType(p.GetValue(oDG, _
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static Or _
Reflection.BindingFlags.GetProperty Or Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.SuppressChangeType, _
Nothing, Nothing, Nothing), Object)
If (Row < propertyinfo =" _" object =" _" type =" _" height =" height">
▼質問
DataGridのヘッダを表示すると列の幅が変更できるんだ。これを、固定表示にしたいんだ。
どうやったらできるか教えてくれ!
▼回答
DataGridには、列の幅を固定にするプロパティはないんだ。だから固定にはできないんだ。
だけど、カスタムコントロールを作成して列の幅の変更操作をできないようにすればできるよ。
その方法の一部を下記に示しておくから参考にしてくれ!
DataGridTextBoxColumnの参考例
Public MyWidth As Integer = 0
Public MyWidthFlag As Boolean = False
'''
''' コントロールの幅を取得または設定します。
'''
''' コントロールの幅。
Public Property CustomWidth() As Integer
Get
Return MyWidth
End Get
Set(ByVal value As Integer)
Me.MyWidth = value
End Set
End Property
'''
''' コントロールの幅を固定にできるかの有無を示す値を取得または設定します。
''' 幅を固定にできる場合は true。それ以外の場合は false。既定値は false です。
'''
''' コントロールの幅を固定にできるかの有無。
Public Property CustomWidthFlag() As Boolean
Get
Return MyWidthFlag
End Get
Set(ByVal value As Boolean)
Me.MyWidthFlag = value
End Set
End Property
Private Sub DataGridCustomTextBoxColumn_WidthChanged( _
ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.WidthChanged
Dim obj As DataGridCustomTextBoxColumn = _
CType(sender, DataGridCustomTextBoxColumn)
If (obj.MyWidthFlag) Then obj.Width = obj.MyWidth
End Sub
■参考サイト
DataGridの列の幅をユーザーが変更できないようにする: .NET Tips: C#, VB.NET, Visual Studio
Dim dg As DataGrid = CType(sender, DataGrid)
Dim row As Integer = dg.CurrentCell.RowNumber : Dim col As Integer = dg.CurrentCell.ColumnNumber
If (dg.TableStyles.Item(0).GridColumnStyles(col).GetType.Name = "DataGridBoolColumn") Then
Dim BoolColumn As DataGridBoolColumn = _
CType(dg.TableStyles.Item(0).GridColumnStyles(col), DataGridBoolColumn)
Dim Bind As BindingManagerBase = _
Me.BindingContext(DirectCast(dg.DataSource, DataTable))
Dim value As Boolean = False
Dim drv As DataRowView = _
CType(DirectCast(Bind.Current, DataRowView), DataRowView)
Dim value As Boolean = _
CBool(IIf(CType(BoolColumn.PropertyDescriptor.GetValue(drv), Boolean), _
False, True))
BoolColumn.PropertyDescriptor.SetValue(drv, value)
End If
BoolColumn.DataGridTableStyle.SelectionBackColor = SystemColors.Window
▼質問
DataGridを編集可能にすると新規追加行(*行)が表示されるんだ。
これを表示しないようにする方法はないのか教えてくれ!
▼回答
あるよ!下記のサイトを参考にしてくれ!
■参考サイト
DataGridの一番下の新しい行(*行)が表示されないようにする: .NET Tips: C#, VB.NET, Visual Studio
http://dobon.net/vb/dotnet/datagrid/allownew.html
おーすばらしい!!できた。
関数にしてみた。
Public Sub SetAllowNew(ByVal b As Boolean, ByVal oDG As DataGrid)
Dim objCurrencyManager As CurrencyManager = _
CType(oDG.BindingContext(oDG.DataSource, oDG.DataMember), CurrencyManager)
'DataViewを取得する
Dim objDataView As DataView = CType(objCurrencyManager.List, DataView)
'新しい行の追加を設定する
objDataView.AllowNew = b
End Sub▼質問
「CurrencyManager」って何?▼回答オブジェクトのプロパティ値とコントロールのプロパティ値との間の単純バインディングの
リストを管理するオブジェクトらしい。
▼質問
新規追加行は表示されずに編集は可能になったんだけど・・・。
これだと、全てのセルが編集可能になってしまって困るんだ。
特定のセルだけを編集可能にするにはどうしたらいいの?
▼回答
DataGridのCurrentCellChangedイベント、MouseClickイベント、MouseDownイベント、
MouseUpイベントでカレントセルをチェックするればできるよ。
こんな関数を作成してみたから、参考にしてくれ!
各イベントで呼ぶだけでOKだ!!
Private Sub CheckCurrentCell(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs)
Dim dg As DataGrid = CType(sender, DataGrid)
Dim row As Integer = dg.CurrentCell.RowNumber
Dim col As Integer = dg.CurrentCell.ColumnNumber
Select Case dg.CurrentCell.ColumnNumber
Case 対象列番号
Case Else
End Select
End Sub
Public Class DataGridCustomTextBoxColumn
Inherits DataGridTextBoxColumn
Private MyBackColor As Color = SystemColors.Window
Private MyForeColor As Color = SystemColors.WindowText
'/******************************************************************************/
' Paint処理
'/******************************************************************************/
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal bounds As Rectangle, _
ByVal [source] As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal backBrush As Brush, _
ByVal foreBrush As Brush, _
ByVal alignToRight As Boolean)
'基本クラスのPaintメソッドを呼び出す。
MyBase.Paint(g, bounds, source, rowNum, _
New SolidBrush(MyBackColor), _
New SolidBrush(MyForeColor), alignToRight)
End Sub
'''
''' コントロールの背景色を取得または設定します。
'''
'''コントロールの背景色を表す System.Drawing.Color。
Public Property BackColor() As Color
Get
Return MyBackColor
End Get
Set(ByVal value As Color)
MyBackColor = value
End Set
End Property
'''
''' コントロールの前景色を取得または設定します。
'''
'''コントロールの前景色を表す System.Drawing.Color。
Public Property ForeColor() As Color
Get
Return MyForeColor
End Get
Set(ByVal value As Color)
MyForeColor = value
End Set
End Property
End Class
■参考サイト
DataGrid内の特定のセルの色を変える: .NET Tips: C#, VB.NET, Visual Studio
http://dobon.net/vb/dotnet/datagrid/coloredcell.html