SAK 図書館
VB テクニック編14 - 動的コントロール追加、クリスタルレポート印刷
■SAK 関数利用規程
・テクニック編で紹介する関数は、私こと Y.SAK の開発関数である。
・著作権明示部分の改編は認めない。
・個人、企業がこれらの関数を使用したり、一部を使用して新たなシステムや
プログラムを開発することは自由です。
・但し、これらの関数を一部でも使用しているソフトウェアをシェアウェア、
その他有償プロダクトとして配布・販売するには、私の許可が必要です。
(無償のフリーソフトウェアなら、自由に配布しても良い。)
・これらの関数を使用して発生した、いかなる形での損害も私こと Y.SAK は
賠償しません。
■動的コントロール追加
・動的コントロールを追加するには、次のようにする。
イベントコードを記述するときは、WithEvents 宣言する。
まず、General - Declarations の Option Explicit 下で動的コントロール
オブジェクトの宣言をする。(動的コントロール作成)
尚、コントロール配列は動的には作成できないようである。
(コントロール配列も動的に作成できると情報を頂戴しました。)
Option Explicit
Private WithEvents DynButton As CommandButton
・動的コントロールを実際に追加するコードは、次のようになる。
プロパティは、追加するコントロールで必要なものを設定する。
Set DynButton = Form1.Controls.Add("VB.CommandButton", "DynButton")
DynButton.Caption = "動的コントロールボタン"
DynButton.Left = 400
DynButton.Top = 100
DynButton.Width = 2000
DynButton.Height = 400
DynButton.Visible = True
・動的コントロールのイベントコードは、次のようにする。
Private Sub DynButton_Click()
msgbox "DynButton クリック"
End Sub
■クリスタルレポート印刷
・クリスタルレポート(Crystal Reports) で印刷する場合、データコントロー
ルとのオートメーションを使用するか、クリスタルレポートでレコードセッ
トを作成するか選択できる。
・定義帳票パターンを使用するには、次の ReportSource = 0 'crptReport に
設定して、クリスタルレポートでレコードセットを作成する。
但し、ODBC アクセスなどのリモートデータベースでないと、
Report1.SQLQuery は指定できない。
(MDB を普通に参照して、クリスタルレポートでレコードセットを作成する
場合の例は、中程に別途サンプルコードがあります。)
MDB でも、ODBC 経由でアクセスすれば、Oracle や SQL-Server と同じに
次のコードが使用できる。
dim dsn as string
dim rti as string
dim rpt as string
dim ppre as string
dim cpis as string
dim dsav as string
dim sql as string
dsn = "dsn=SAK3_ADO;uid=SAK;pwd=SAK"
rti = "レポートタイトル"
rpt = "l_juchu.rpt"
ppre = 0 'プレビュー
cpis = 1
dsav = 1 'データ保存
sql = "select * from 受注m where 品番 = 'a001' order by 受注番号"
'** 印刷
Report1.ReportFileName = rpt
Report1.ReportSource = 0 'crptReport
Report1.Connect = dsn
Report1.Destination = ppre
Report1.PrinterCopies = cpis
Report1.DiscardSavedData = dsav
Report1.SQLQuery = sql
Report1.WindowTitle = rti
On Error Resume Next
Report1.Action = 1
If err <> 0 Then
msgbox "作表でエラーが発生しました。"
End If
On Error GoTo 0
・MDB を普通に参照して、クリスタルレポートでレコードセットを作成する場
合は、ReportSource = 0 'crptReport として、次のようにコードする。
dim rti as string
dim rpt as string
dim ppre as string
dim cpis as string
dim dsav as string
dim where as string
rti = "レポートタイトル"
rpt = "l_juchu.rpt"
ppre = 0 'プレビュー
cpis = 1
dsav = 1 'データ保存
where = "{受注M.品番} = 'a001' and {受注M.得意先CD} = '2000'"
'** 印刷
Report1.ReportFileName = rpt
Report1.ReportSource = 0 'crptReport
Report1.Destination = ppre
Report1.PrinterCopies = cpis
Report1.DiscardSavedData = dsav
Report1.SelectionFormula = where
Report1.WindowTitle = rti
On Error Resume Next
Report1.Action = 1
If err <> 0 Then
msgbox "作表でエラーが発生しました。"
End If
On Error GoTo 0
・データコントロール(ADC 等) とオートメーションリンクする場合は、
ReportSource = 3 'crptDataControl
と指定する。(古いパージョンは、ADC には対応していない。)
但し、帳票定義パターンは使用できない。
dim rti as string
dim rpt as string
dim ppre as string
dim cpis as string
dim dsav as string
dim sql as string
rti = "レポートタイトル"
rpt = "l_juchu.rpt"
ppre = 0 'プレビュー
cpis = 1
dsav = 1 'データ保存
sql = "select * from 受注m where 得意先CD = '2000'"
Data1.RecordSource = sql
Data1.Refresh
'** 印刷
Report1.ReportFileName = rpt
Report1.ReportSource = 3 'crptDataControl
Report1.Destination = ppre
Report1.PrinterCopies = cpis
Report1.DiscardSavedData = dsav
Report1.WindowTitle = rti
On Error Resume Next
Report1.Action = 1
If Err <> 0 Then
MsgBox "作表でエラーが発生しました。"
End If
On Error GoTo 0
■VB テクニック編資料
■VB 入門編資料
■VB 基礎編資料
■VB ビジュアル編資料