Function BuildQuery(mark)
q_i = Sheets("登録者").Range("B21").Value
q_c = Sheets("登録者").Range("B3").Value
q_m = Sheets("登録者").Range("B5").Value
q_s = Sheets("登録者").Range("B7").Value
q_q = Sheets("登録者").Range("B9").Value
q_n = Sheets("登録者").Range("B11").Value
q_i = StrConv(q_i, vbNarrow)
q_s = StrConv(q_s, vbNarrow)
q_q = StrConv(q_q, vbNarrow)
q_i = Replace(q_i, "'", "’")
q_c = Replace(q_c, "'", "’")
q_m = Replace(q_m, "'", "’")
q_s = Replace(q_s, "'", "’")
q_q = Replace(q_q, "'", "’")
q_n = Replace(q_n, "'", "’")
q_i = Replace(q_i, """", "’")
q_c = Replace(q_c, """", "’")
q_m = Replace(q_m, """", "’")
q_s = Replace(q_s, """", "’")
q_q = Replace(q_q, """", "’")
q_n = Replace(q_n, """", "’")
If mark = "s1" Then q = "select ID,customer,model,serial,quantity,notes from orders where visible = 'true'"
If mark = "s2" Then q = "select ID,customer,model,serial,quantity,notes from orders where visible = 'false'"
If mark = "i" Then q = "insert into orders (customer,model,serial,quantity,notes,visible) values ('" & q_c & "','" & q_m & "','" & q_s & "'," & q_q & ",'" & q_n & "','true');"
If mark = "u" Then q = "update orders set customer = '" & q_c & "', model = '" & q_m & "', serial = '" & q_s & "', quantity = " & q_q & ", notes = '" & q_n & "' where ID = " & q_i & ";"
If mark = "d" Then q = "delete from orders where ID = " & q_i & ";"
If mark = "h" Then q = "update orders set visible = 'false' where ID = " & q_i & ";"
BuildQuery = q: Debug.Print q
End Function
Private Sub CommandButton6_Click()
If db_chk = False Then Exit Sub
grid_clear
If grid_load("s1") = False Then Exit Sub
form_clear
End Sub
Private Sub CommandButton5_Click()
If db_chk = False Then Exit Sub
grid_clear
If grid_load("s2") = False Then Exit Sub
form_clear
End Sub
Private Sub CommandButton1_Click()
If db_chk = False Then Exit Sub
If form_chk = False Then Exit Sub
If qty_parse = False Then Exit Sub
If form_query("i") = False Then Exit Sub
grid_clear
If grid_load("s1") = False Then Exit Sub
form_clear
End Sub
Private Sub CommandButton2_Click()
If db_chk = False Then Exit Sub
If id_chk = False Then Exit Sub
If form_chk = False Then Exit Sub
If qty_parse = False Then Exit Sub
If form_query("u") = False Then Exit Sub
grid_clear
If grid_load("s1") = False Then Exit Sub
form_clear
End Sub
Private Sub CommandButton3_Click()
If db_chk = False Then Exit Sub
If id_chk = False Then Exit Sub
yn = MsgBox("削除しますか?", vbYesNo)
If yn = vbNo Then Exit Sub
If form_query("d") = False Then Exit Sub
grid_clear
If grid_load("s1") = False Then Exit Sub
form_clear
End Sub
Private Sub CommandButton4_Click()
If db_chk = False Then Exit Sub
If id_chk = False Then Exit Sub
If form_query("h") = False Then Exit Sub
grid_clear
If grid_load("s1") = False Then Exit Sub
form_clear
End Sub
Function id_chk()
On Error GoTo try
If Sheets("登録者").Range("B21").Value = "" Then GoTo try
v = CLng(Sheets("登録者").Range("B21").Value)
id_chk = True
Exit Function
try:
MsgBox "IDが正しくありません。"
id_chk = False
End Function
Function form_chk()
If Sheets("登録者").Range("B3").Value = "" Then GoTo try
If Sheets("登録者").Range("B5").Value = "" Then GoTo try
If Sheets("登録者").Range("B7").Value = "" Then GoTo try
If Sheets("登録者").Range("B9").Value = "" Then GoTo try
form_chk = True
Exit Function
try:
MsgBox "入力が不足しています。"
form_chk = False
End Function
Sub grid_clear()
Sheets("登録者").Range("G4:L1000").Delete
End Sub
Sub form_clear()
Sheets("登録者").Range("B3").Value = ""
Sheets("登録者").Range("B5").Value = ""
Sheets("登録者").Range("B7").Value = ""
Sheets("登録者").Range("B9").Value = ""
Sheets("登録者").Range("B11").Value = ""
Sheets("登録者").Range("B21").Value = ""
End Sub
Function qty_parse()
On Error GoTo try
qty = CLng(Sheets("登録者").Range("B9").Value)
If qty = 0 Then GoTo try
qty_parse = True
Exit Function
try:
MsgBox "数量が正しくありません。"
qty_parse = False
End Function
Function form_query(mark)
On Error GoTo try
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Sheets("設定").Range("B3").Value & ";"
cn.Execute BuildQuery(mark)
If cn.state = 1 Then cn.Close
Set cn = Nothing
form_query = True
Exit Function
try:
If cn.state = 1 Then cn.Close
Set cn = Nothing
MsgBox Err.Description
form_query = False
End Function
Function grid_load(mark)
On Error GoTo try
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Sheets("設定").Range("B3").Value & ";"
rs.Open BuildQuery(mark), cn
r = 4
Do Until rs.EOF
Sheets("登録者").Cells(r, 7).Value = rs(0)
Sheets("登録者").Cells(r, 8).Value = rs(1)
Sheets("登録者").Cells(r, 9).Value = rs(2)
Sheets("登録者").Cells(r, 10).Value = rs(3)
Sheets("登録者").Cells(r, 11).Value = rs(4)
Sheets("登録者").Cells(r, 12).Value = rs(5)
Sheets("登録者").Range("G" & r & ":L" & r & "").Borders.LineStyle = True
r = r + 1
rs.movenext
Loop
If rs.state = 1 Then rs.Close
Set rs = Nothing
If cn.state = 1 Then cn.Close
Set cn = Nothing
grid_load = True
Exit Function
try:
If rs.state = 1 Then rs.Close
Set rs = Nothing
If cn.state = 1 Then cn.Close
Set cn = Nothing
grid_load = False
End Function
Function db_chk()
On Error GoTo try
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Sheets("設定").Range("B3").Value & ";"
If cn.state = 1 Then cn.Close
Set cn = Nothing
db_chk = True
Exit Function
try:
If cn.state = 1 Then cn.Close
Set cn = Nothing
MsgBox "DBに接続できません。"
db_chk = False
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row >= 4 And Target.Column = 7 And Target.Text <> "" Then
ActiveSheet.Cells(3, 2).Value = ActiveSheet.Cells(Target.Row, 8).Value
ActiveSheet.Cells(5, 2).Value = ActiveSheet.Cells(Target.Row, 9).Value
ActiveSheet.Cells(7, 2).Value = ActiveSheet.Cells(Target.Row, 10).Value
ActiveSheet.Cells(9, 2).Value = ActiveSheet.Cells(Target.Row, 11).Value
ActiveSheet.Cells(11, 2).Value = ActiveSheet.Cells(Target.Row, 12).Value
ActiveSheet.Cells(21, 2).Value = ActiveSheet.Cells(Target.Row, 7).Value
ElseIf Target.Row >= 4 And Target.Column = 7 And Target.Text = "" Then
ActiveSheet.Cells(3, 2).Value = ""
ActiveSheet.Cells(5, 2).Value = ""
ActiveSheet.Cells(7, 2).Value = ""
ActiveSheet.Cells(9, 2).Value = ""
ActiveSheet.Cells(11, 2).Value = ""
ActiveSheet.Cells(21, 2).Value = ""
End If
End Sub