AccessVBA入門⑧フォーム・レポートの応用操作

Access

KeyPress

Private Sub txt1_KeyPress(KeyAscii As Integer)
  Select Case KeyAscii
  Case 8,9,13
    Exit Sub
  Case Else
    IF KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then
      Me.lbl1.Caption = "数字キーが押されました"
    ElseIf KeyAscii >= Asc("a") And KeyAscii <= Asc("z") Then
      Me.lbl1.Caption = "英字キーが押されました"
    Else
      Me.lbl1.Caption = "キーの入力をキャンセルしました"
      KeyAscii = 0
    End If
  End Select
End Sub

GotFocus

Private Sub txt2_GotFocus()
  If IsNull(Me.txt2.Value)Then
    Me.txt2.Value = Me.txt1.Value
    Me.txt2.SelStart = Nz(Len(Me.txt1.Value),0)
  End If
End Sub

Before Update

Private Sub txt3_BeforeUpdate(Cancel As Integer)
  If IsNumeric(Me.txt3.Value) Then
    If CDbl(Me.txt3.Value) = CLng(Me.txt3.Value) Then
      Me.lbl2.Caption = "整数が入力されています"
      Exit Sub
    End If
  Else
    Me.lbl2.Caption = "整数以外の値が入力されています"
  End If
  Cancel = True
  Me.txt3.SelStart = 0
  Me.txt3.SelLength = Nz(Len(Me.txt3.Value),0)
End Sub
Private Sub txt4_BeforeUpdate(Cancel As Integer)
  If Len(Me.txt4.Value) <> LenB(StrConv(Me.txt4.Value,vbFromUnicode)) Then
    Me.lbl3.Caption = "半角文字が入力されています"
    Cancel = True
    Me.txt4.SelStart = 0
    Me.txt4.SelLength = Nz(Len(Me.txt4.Value),0)
  Else
    Me.lbl3.Caption = "全角文字だけが入力されています"
  End If
End Sub

NotInList

Private Sub cmb1_NotInList(NewData As String, Response As Integer)
  If MsgBox("入力されたデータを登録しますか?", vbYesNo) = vbYes Then
    DoCmd.OpenForm "F応用2", , , , acFormAdd, acDialog, NewData
  End If

  If DCount("部署コード", "T部署マスタ", "部署コード='" & NewData & "'") <> 0 Then
    Response = acDataErrAdded
  Else
    Response = acDataErrContinue
    Me.cmb1.Undo
  End If
End Sub
Private Sub Form_Open(Cancel As Integer)
  If IsNull(Me.OpenArgs) Then
    Cancel = True
  End If
End Sub
Private Sub Form_Load()
  Me.部署コード.Value = StrConv(Me.OpenArgs, vbUpperCase)
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
  If IsNull(Me.部署名.Value) Then
    Cancel = True
  End If
End Sub

Private Sub Form_After Insert()
  DoCmd.Close acForm Me.Name
End Sub

Private Sub 部署名_KeyPress(KeyAscii As Integer)
  If KeyAscii = 27 Then
    DoCmd.Close acForm Me.Name
  End If
End Sub

Load

Private Sub Form_Load()
  Dim o As Object
  For Each o In Application.Printers
    Me.lst1.AddItem o.DeviceName
  Next o
  Me.lst1.Value = Application.Printer.DeviceName
End Sub

Private Sub btn1_Click()
  Dim MyPrinter As Object
  If MsgBox("選択されたプリンタで印刷しますか?", vbYesNo) = vbYes Then
    Set MyPrinter = Application.Printer
    Set Application.Printer = Application.Printers(Me.lst1.Value)
    DoCmd.OpenReport "R応用3"
    Set Application.Printer = MyPrinter
    Set MyPrinter = Nothing
  End If
End Sub

コメント

タイトルとURLをコピーしました