プロが教えるわが家の防犯対策術!

vbaレポート名の指定が間違っている!。
stdocnameの中身も確認しました。
でも下消又は上消の処理へ行くと上記エラーが表示されます。
この文のReports!stdocname!直線103.Visible = True書き方がおかしいようです。
どなたかご指南いただけますでしょうか。
よろしくお願い致します。

'*** stdocname(判定で指定されたレポート)を仮に開く
DoCmd.OpenReport stdocname, acViewPreview

'*** 消線の種類で印刷処理選択/消無は通常印刷 ---> 消線と月消を判定
If Forms!メイン画面![リストDELSEN] = "下消" Then
GoTo SetLowerLine
End If

If Forms!メイン画面![リストDELSEN] = "上消" Then
GoTo SetUpperLine
End If

GoTo Insatumae

'*** 下消処理
SetLowerLine:
Reports!stdocname!直線103.Visible = True
Reports!stdocname!直線104.Visible = True
Reports!stdocname!直線105.Visible = True
Reports!stdocname!直線106.Visible = True
Reports!stdocname!直線107.Visible = True

'*** 下消時の月消処理(00は処理しない)
Format ("00")
ii = Forms!メイン画面![テキストDELTUKI]
If ii = "00" Then
Else
Reports!stdocname!("直線A" & "ii").Visible = True
Reports!stdocname!("直線B" & "ii").Visible = True
Reports!stdocname!("直線C" & "ii").Visible = True
End If

GoTo Insatumae

'*** 上消処理
SetUpperLine:
Reports!stdocname!直線103.Visible = True
Reports!stdocname!直線104.Visible = True
Reports!stdocname!直線105.Visible = True
Reports!stdocname!直線106.Visible = True
Reports!stdocname!直線107.Visible = True

'*** 上消時の月消処理(00は処理しない)
Format ("00")
ii = Forms!メイン画面![テキストDELTUKI]
If ii = "00" Then
Else
Reports!stdocname!("直線A" & "ii").Visible = True
Reports!stdocname!("直線B" & "ii").Visible = True
Reports!stdocname!("直線C" & "ii").Visible = True
End If

GoTo Insatumae

'*** 印刷前処理
Insatumae:

A 回答 (2件)

No.1です。



まず、前回の訂正から(汗)

前回回答の末尾で
> Reports(stdocname).Controls("直線A" & "ii").Visible = True
としましたが、「Set objRep = ~」の実行後はここでもobjRepを使用して
  objRep.Controls("直線A" & ii).Visible = True
というようにします。
(前回のように「Reports(~)」を使用してもエラーにはなりませんが)

なお、前回気づいていなかったのですが、「Controls(~)」の中の「ii」は
変数なので、そちらは「"」(ダブルクォート)で括らず、上記のように
「Controls("直線A" & ii)」としてください。
(こちらはそうしないとエラー('2465')になります)


> '2451'から明細#errorとなります。

こちらで確認したところ、「Set objRep = Reports(stdocname)」の実行時に
stdocnameで指定したレポートが開いていないと、このエラー('2451')となる
ようです。

> '*** stdocname(判定で指定されたレポート)を仮に開く
> DoCmd.OpenReport stdocname, acViewPreview
> Set objrep = Reports(stdocname)

・・・レポートを開いた直後、ですか・・・(汗)
これだと、「stdocname」に実在しないレポート名を指定していたら、レポートを
開こうとした時点でエラーが発生するはずなのですが・・・(汗)

念のための確認になりますが、「On Error GoTo Err_印刷_Click」で飛ばす
エラー処理の中で、「実行時エラー'2497'」(レポート名に空文字を指定した
場合などに発生するエラー)に対する処理を組んでいないでしょうか。


もしそうでしたら、
 a)エラー処理内で「2497」エラーと同様の処理を「2451」に対しても実行する、
 b)「Select Case」の最後に「Case Else」(その前のCaseに該当しない場合)
  の処理として「Exit Sub」を追加する、
といった対処をすることになるかと思います。

上記の予想(=stdocnameに正しいレポート名が指定されていない場合にエラー
処理を組んだ上で「Resume Next」で処理を継続している)が外れていた場合は、
ちょっと原因の推測ができません(汗)
(・・・或いは、レポート側の「開く時」(Openイベント)で、閉じるかキャンセルが
 入っている・・・ということはないですよね?(汗))

この回答への補足

(1)."ii"の件。
Reports(stdocname).Controls("直線A" & "ii").Visible = True

objrep.Controls("直線A" & ii).Visible = True
に修正しました。
以下、実行後、プレビュー(#Errorのまま)はされますが、
'2465'指定した式で参照されている'直線A0'フィールドが見つかりません。となります。

(2).・・・レポートを開いた直後、ですか・・・(汗)
これだと、「stdocname」に実在しないレポート名を指定していたら、レポートを
開こうとした時点でエラーが発生するはずなのですが・・・(汗)の件。

'*** 上段・下段使用と対象月から使用レポートを判定
Select Case joge & tuki
Case "上段" & "1"
stdocname = "レポート01上"



Case "下段" & "12"
stdocname = "レポート12下"
Case Else
stdocname = "レポート原稿"
End Select

'*** stdocname(判定で指定されたレポート)を仮に開く
DoCmd.OpenReport stdocname, acViewPreview
Set objrep = Reports(stdocname)
上記の様に'case else'で対処しております。

(3).上記の予想(=stdocnameに正しいレポート名が指定されていない場合にエラー
処理を組んだ上で「Resume Next」で処理を継続している)が外れていた場合は、
ちょっと原因の推測ができません(汗)の件。
'*** 上消時の月消処理(00は処理しない)
Private Sub 印刷_Click()
On Error GoTo Err_印刷_Click

'*** 変数の宣言
Dim stdocname As String
Dim objrep As Report
Dim delt As String
Dim ii As String
Dim tuki As String
Dim mais As Integer
Dim denp As Integer
Dim busu As Integer
Dim joge As String
Dim kmsg As String



'*** 上段・下段使用と対象月から使用レポートを判定
Select Case joge & tuki
Case "上段" & "1"
stdocname = "レポート01上"



Format ("00")
ii = Forms!メイン画面![テキストDELTUKI]
If ii = "00" Then
Else
objrep.Controls("直線A" & ii).Visible = True
objrep.Controls("直線B" & ii).Visible = True
objrep.Controls("直線C" & ii).Visible = True
End If

GoTo Insatumae

'*** 印刷前処理
Insatumae:

'*** stdocname(判定で指定されたレポート)を最大プレビューで開く
DoCmd.OpenReport stdocname, acViewPreview
DoCmd.Maximize



'*** エラーを無視して出口へ
Err_印刷_Click:
MsgBox (Err.Number & " " & Err.Description)
MsgBox Err.Description
kmsg = MsgBox("印刷時にエラーが発生しました!", vbCritical, "警告メッセージ")
Resume Exit_印刷_Click
resumeで対応しております。

補足日時:2010/03/23 11:29
    • good
    • 0
この回答へのお礼

ありがとうございます。
まで修正してみてご返答致します。

お礼日時:2010/03/23 11:04

> この文のReports!stdocname!直線103.Visible = True書き方がおかしいようです。



「stdocname」はString型の変数だと思いますので、最も単純には上記の式は
Reports(stdocname)!直線103.Visible = True
になります。

ただ、ご質問のコードでは、これに該当するものがかなりの数ありますので、
オブジェクト型の変数の宣言を追加したり、Withを使用した方がよいかと
思います。

【現状(一部のみ抜粋)】
'*** 下消処理
SetLowerLine:
Reports!stdocname!直線103.Visible = True
Reports!stdocname!直線104.Visible = True
Reports!stdocname!直線105.Visible = True
Reports!stdocname!直線106.Visible = True
Reports!stdocname!直線107.Visible = True


【上記を正しい構文に書換】
'*** 下消処理
SetLowerLine:
Reports(stdocname)!直線103.Visible = True
Reports(stdocname)!直線104.Visible = True
Reports(stdocname)!直線105.Visible = True
Reports(stdocname)!直線106.Visible = True
Reports(stdocname)!直線107.Visible = True


【オブジェクト型の変数を使用した書換】
'以下は「Dim stdocname As String」を宣言しているのと同じ辺りに追加
'(レポートオブジェクト型の変数の宣言)
Dim objRep As Report

'以下は「stdocname」にレポート名を代入した後に追加
Set objRep = Reports(stdocname)

'*** 下消処理
SetLowerLine:
objRep!直線103.Visible = True
objRep!直線104.Visible = True
objRep!直線105.Visible = True
objRep!直線106.Visible = True
objRep!直線107.Visible = True

'以下はこのコードを記述しているSubの終わる直前に追加(メモリの解放)
Set objRep = Nothing


【さらにWithを使用した書換】
'*** 下消処理(上の「【オブジェクト型の変数を使用】」 の下消処理を差替)
'(上記の「Dim objRep As Report」等の行はこちらでも必要。あくまで「下消
' 処理」のみを差替になります)
SetLowerLine:
'※「With ○○ ~ End With」の間に書かれたコードでは、「!」や「.」で始まる
  ものは、「○○!~」「○○.~」と指定されたのと同じ動作になります。
With objRep
  !直線103.Visible = True
  !直線104.Visible = True
  !直線105.Visible = True
  !直線106.Visible = True
  !直線107.Visible = True
End With


・・・以上です。

なお「上消時の月消処理」のところで

> Reports!stdocname!("直線A" & "ii").Visible = True

という記述がありますが、これも以下のような修正が必要です:

【正】
Reports(stdocname).Controls("直線A" & "ii").Visible = True

この回答への補足

'2451'から明細#errorとなります。
ご指摘頂いた箇所を修正してみたところ、上記のようになりました。

Private Sub 印刷_Click()
On Error GoTo Err_印刷_Click

'*** 変数の宣言
Dim stdocname As String
Dim objrep As Report



'*** 上段・下段使用と対象月から使用レポートを判定
Select Case joge & tuki
Case "上段" & "1"
stdocname = "レポート01上"
Case "上段" & "2"
stdocname = "レポート02上"



End Select

'*** stdocname(判定で指定されたレポート)を仮に開く
DoCmd.OpenReport stdocname, acViewPreview
Set objrep = Reports(stdocname)

'*** 消線の種類で印刷処理選択/消無は通常印刷 ---> 消線と月消を判定
If Forms!メイン画面![リストDELSEN] = "下消" Then
GoTo SetLowerLine
End If

If Forms!メイン画面![リストDELSEN] = "上消" Then
GoTo SetUpperLine
End If

GoTo Insatumae

'*** 下消処理
SetLowerLine:
With objrep
!直線103.Visible = True
!直線104.Visible = True
!直線105.Visible = True
!直線106.Visible = True
!直線107.Visible = True
End With

'*** 下消時の月消処理(00は処理しない)
Format ("00")
ii = Forms!メイン画面![テキストDELTUKI]
If ii = "00" Then
Else
Reports(stdocname).Controls("直線A" & "ii").Visible = True
Reports(stdocname).Controls("直線B" & "ii").Visible = True
Reports(stdocname).Controls("直線C" & "ii").Visible = True
End If

GoTo Insatumae



'*** 上消時の月消処理(00は処理しない)
Format ("00")
ii = Forms!メイン画面![テキストDELTUKI]
If ii = "00" Then
Else
Reports(stdocname).Controls("直線A" & "ii").Visible = True
Reports(stdocname).Controls("直線B" & "ii").Visible = True
Reports(stdocname).Controls("直線C" & "ii").Visible = True
End If

GoTo Insatumae

'*** 印刷前処理
Insatumae:

'*** stdocname(判定で指定されたレポート)を最大プレビューで開く
DoCmd.OpenReport stdocname, acViewPreview
DoCmd.Maximize

補足日時:2010/03/22 14:47
    • good
    • 0
この回答へのお礼

ありがとうございます。
以下修正してみます。

お礼日時:2010/03/22 14:22

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています