1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
Sub CellsCondition() ' 条件付書式クリア Set w = Sheets("11月") 'w.Cells.FormatConditions.Delete w.Columns("J").FormatConditions.Delete ' 最終行取得 'b = w.UsedRange.Rows.Count b = 324 For r = 2 To b For c = 10 To 10 ' J列のみ ' A列の値(Weekday)が1なら赤 'Set f = w.Cells(r, c).FormatConditions.Add(xlExpression, xlEqual, "=Weekday(A" & r & ")=1") 'f.Interior.Color = RGB(255, 200, 200) 'f.StopIfTrue = False ' A列の値(Weekday)が7なら青(複数条件) 'Set f = w.Cells(r, c).FormatConditions.Add(xlExpression, xlEqual, "=AND(B" & r & "=0,Weekday(A" & r & ")=7)") 'f.Interior.Color = RGB(200, 200, 255) 'f.StopIfTrue = False ' 重複チェック+特定の単語を除外 'Set f = w.Cells(r, c).FormatConditions.Add(xlExpression, xlEqual, "=AND(COUNTIF(J2:J324,J" & r & ")>1,J" & r & "<>""シコミ"")") 'f.Interior.Color = RGB(255, 0, 0) 'f.StopIfTrue = False ' A列の値が1なら書式設定 'Set f = w.Cells(r, 1).FormatConditions.Add(xlExpression, xlEqual, "=Day(A" & r & ")=1") 'f.NumberFormat = "mm/dd(aaa)" 'f.StopIfTrue = False Next c Next r End Sub |
C# FFmpegにコマンド投げる②
ちょっと動画の長さを調整したいとき用。
実行ファイルと同じ場所にffmpeg.exeを置く。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Runtime.InteropServices; using QuartzTypeLib; namespace FFMPEG_UI { public partial class Form1 : Form { public Form1() { InitializeComponent(); listBox1.AllowDrop = true; textBox1.Text = "00"; textBox2.Text = "00"; textBox3.Text = "00"; textBox4.Text = "000"; textBox5.Text = "00"; textBox6.Text = "00"; textBox7.Text = "00"; textBox8.Text = "000"; } private void listBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy; } private void listBox1_DragDrop(object sender, DragEventArgs e) { listBox1.Items.AddRange((string[])e.Data.GetData(DataFormats.FileDrop, false)); } private void button2_Click(object sender, EventArgs e) { listBox1.Items.Clear(); textBox1.Text = "00"; textBox2.Text = "00"; textBox3.Text = "00"; textBox4.Text = "000"; textBox5.Text = "00"; textBox6.Text = "00"; textBox7.Text = "00"; textBox8.Text = "000"; } private void button1_Click(object sender, EventArgs e) { string s_h = textBox1.Text; string s_m = textBox2.Text; string s_s = textBox3.Text; string s_ms = textBox4.Text; string e_h = textBox5.Text; string e_m = textBox6.Text; string e_s = textBox7.Text; string e_ms = textBox8.Text; TimeSpan start_position = new TimeSpan(0, int.Parse(s_h), int.Parse(s_m), int.Parse(s_s), int.Parse(s_ms)); TimeSpan end_position = new TimeSpan(0, int.Parse(e_h), int.Parse(e_m), int.Parse(e_s), int.Parse(e_ms)); string interval = (end_position - start_position).TotalSeconds.ToString(); foreach (string s in listBox1.Items) { string filePath = System.IO.Path.GetDirectoryName(s) + @"\out_" + System.IO.Path.GetFileName(s); System.Diagnostics.Process p = System.Diagnostics.Process.Start( Application.StartupPath + @"\ffmpeg.exe" , "-ss " + start_position.TotalSeconds.ToString() + " -i \"" + s.ToString() + "\" -t " + interval + " \"" + filePath + "\""); p.WaitForExit(); } } } } |
VBA 工数管理プログラム 変更版
前回作成したファイルにマスタ管理機能を追加しフォームを少し変更。
Module1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
Sub auto_open() SetStyle If (StrConv(Sheets("設定").Range("B5").Value, 10) = "on") Then Sheets("作業登録").Activate UserForm1.Show End If On Error Resume Next Application.CommandBars("cell").Controls("フォーム表示").Delete Application.CommandBars("cell").Controls("フォーム表示").Delete With Application.CommandBars("cell").Controls.Add .Caption = "フォーム表示" .OnAction = "FormShow" End With End Sub Sub auto_close() On Error Resume Next Application.CommandBars("cell").Controls("フォーム表示").Delete Application.CommandBars("cell").Controls("フォーム表示").Delete End Sub Sub FormShow() Sheets("作業登録").Activate UserForm1.Show End Sub Sub SetStyle() 'マスタ管理 Sheets("マスタ管理").Cells(3, 2).NumberFormatLocal = "@" '登録者 Sheets("登録者").Cells(3, 2).NumberFormatLocal = "@" Sheets("登録者").Cells(5, 2).NumberFormatLocal = "@" Sheets("登録者").Cells(7, 2).NumberFormatLocal = "@" Sheets("登録者").Cells(11, 2).NumberFormatLocal = "@" '作業登録 Sheets("作業登録").Cells.NumberFormatLocal = "" Sheets("作業登録").Columns("A").NumberFormatLocal = "@" Sheets("作業登録").Columns("B").NumberFormatLocal = "@" For c = 8 To 101 Step 3 Sheets("作業登録").Cells(1, c).Value = "種" Sheets("作業登録").Cells(1, c + 1).Value = "スタート" Sheets("作業登録").Cells(1, c + 2).Value = "ストップ" Sheets("作業登録").Columns(c).NumberFormatLocal = "" Sheets("作業登録").Columns(c + 1).NumberFormatLocal = "mm/dd hh:mm" Sheets("作業登録").Columns(c + 2).NumberFormatLocal = "mm/dd hh:mm" Sheets("作業登録").Columns(c).ColumnWidth = "2.5" Sheets("作業登録").Columns(c + 1).ColumnWidth = "12" Sheets("作業登録").Columns(c + 2).ColumnWidth = "12" Next c End Sub Sub WorkTimeAdd() Set w = Sheets("作業登録") For r = 2 To w.UsedRange.Rows.Count w.Cells(r, 5) = "" w.Cells(r, 6) = "" If w.Cells(r, 4).Text = "完了" And w.Cells(r, 7).Text <> "" Then n = 0 f = 0 For c = 8 To w.Cells(r, 7).Text Step 3 Select Case w.Cells(r, c).Text Case "作" n = n + WorkTime(w.Cells(r, c + 1).Text, w.Cells(r, c + 2).Text) Case "修" f = f + WorkTime(w.Cells(r, c + 1).Text, w.Cells(r, c + 2).Text) End Select Next c w.Cells(r, 5) = Int(n / 60) & "." & Format(n Mod 60, "00") & " H" w.Cells(r, 6) = Int(f / 60) & "." & Format(f Mod 60, "00") & " H" End If Next r w.Activate End Sub Function WorkTime(date_s, date_e) On Error GoTo try date_s = CDate(date_s) date_e = CDate(date_e) ts = 0 Do While date_s < date_e ts = ts + 1 If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 10:00:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 10:15:00") Then ts = ts - 1 End If If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 12:00:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 12:50:00") Then ts = ts - 1 End If If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 15:00:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 15:15:00") Then ts = ts - 1 End If If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 17:20:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 17:30:00") Then ts = ts - 1 End If date_s = DateAdd("n", 1, date_s) Loop WorkTime = ts Exit Function try: WorkTime = 0 End Function |
UserForm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
Function form_query() 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 "select ID,customer,model,serial,quantity,notes from orders where serial = '" & UserForm1.TextBox1.Text & "';", cn Do Until rs.EOF UserForm1.TextBox8.Text = rs(0) UserForm1.TextBox2.Text = rs(1) UserForm1.TextBox3.Text = rs(2) UserForm1.TextBox1.Text = rs(3) UserForm1.TextBox7.Text = rs(4) UserForm1.TextBox4.Text = rs(5) rs.movenext Loop If UserForm1.TextBox8.Text <> "" And UserForm1.TextBox7.Text > 0 Then For i = 1 To UserForm1.TextBox7.Text UserForm1.ComboBox2.AddItem i UserForm1.ComboBox3.AddItem i Next i UserForm1.ComboBox2.Text = "1" UserForm1.ComboBox3.Text = UserForm1.TextBox7.Text End If If rs.state = 1 Then rs.Close Set rs = Nothing If cn.state = 1 Then cn.Close Set cn = Nothing form_query = True Exit Function try: If rs.state = 1 Then rs.Close Set rs = Nothing If cn.state = 1 Then cn.Close Set cn = Nothing MsgBox Err.Description form_query = False End Function Private Sub CommandButton1_Click() Sheets("作業登録").Activate If UserForm1.TextBox1.Text = "" Then MsgBox "工番を入力してください。" Exit Sub End If If db_chk = False Then Exit Sub form_clear form_query End Sub Sub form_clear() UserForm1.TextBox8.Text = "" UserForm1.TextBox2.Text = "" UserForm1.TextBox3.Text = "" UserForm1.TextBox7.Text = "" UserForm1.TextBox4.Text = "" UserForm1.ComboBox2.Clear UserForm1.ComboBox3.Clear UserForm1.ComboBox2.Text = "" UserForm1.ComboBox3.Text = "" UserForm1.ComboBox1.Text = "" End Sub 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 Function qty_parse() On Error GoTo try If UserForm1.ComboBox2.Text = "" Or UserForm1.ComboBox3.Text = "" Then MsgBox "対象枝を入力してください。" qty_parse = False Exit Function End If qty = CInt(UserForm1.TextBox7.Text) suffix_s = CInt(UserForm1.ComboBox2.Text) suffix_e = CInt(UserForm1.ComboBox3.Text) If suffix_s = 0 Or suffix_e = 0 Then MsgBox "対象枝は1以上の数字を指定する必要があります。" qty_parse = False Exit Function End If If suffix_e > qty Or suffix_s > suffix_e Then MsgBox "対象枝の指定範囲に問題があります。" qty_parse = False Exit Function End If qty_parse = True Exit Function try: MsgBox "対象枝が数字ではありません。" qty_parse = False End Function Private Sub CommandButton2_Click() Sheets("作業登録").Activate Call MainWrite("1", "スタート", "作") MsgBox "完了しました。" End Sub Private Sub CommandButton3_Click() Sheets("作業登録").Activate Call MainWrite("1", "スタート", "修") MsgBox "完了しました。" End Sub Private Sub CommandButton4_Click() Sheets("作業登録").Activate Call MainWrite("3", "ストップ", "") MsgBox "完了しました。" End Sub Private Sub CommandButton5_Click() Sheets("作業登録").Activate Call MainWrite("4", "完了", "") MsgBox "完了しました。" End Sub Private Sub CommandButton6_Click() Sheets("作業登録").Activate UserForm1.TextBox1.Text = "" form_clear End Sub Private Sub CommandButton7_Click() Sheets("作業登録").Activate WorkTimeAdd MsgBox "完了しました。" End Sub Sub MainWrite(mark, state, typ) If ActiveSheet.Name <> "作業登録" Then MsgBox "作業登録で実施してください。" Exit Sub End If If UserForm1.TextBox8.Text = "" Then MsgBox "対象のIDが空白です。取得してください。" Exit Sub End If If UserForm1.ComboBox1.Text = "" Then MsgBox "作業者を選んでください。" Exit Sub End If If qty_parse = False Then Exit Sub Set w = Sheets("作業登録") serial = UserForm1.TextBox1.Text worker = UserForm1.ComboBox1.Text For suffix = CInt(UserForm1.ComboBox2.Text) To CInt(UserForm1.ComboBox3.Text) f = False For r = 2 To w.UsedRange.Rows.Count If worker = w.Cells(r, 1).Text And serial = w.Cells(r, 2).Text And suffix = w.Cells(r, 3) Then f = True Call WriteCol(r, worker, serial, suffix, mark, state, typ) w.Cells(r, 1).Select End If Next r If f = False Then Call WriteCol(r, worker, serial, suffix, mark, state, typ) w.Cells(r, 1).Select End If Next suffix End Sub Sub WriteCol(r, worker, serial, suffix, mark, state, typ) Set w = Sheets("作業登録") If w.Cells(r, 4).Value = "完了" Then MsgBox serial & "-" & suffix & "は既に完了しています。" Exit Sub End If Select Case mark Case "1" '作業スタート If w.Cells(r, 4).Value = "スタート" Then MsgBox serial & "-" & suffix & "は既にスタートしています。" Exit Sub End If If w.Cells(r, 4).Value = "" Or w.Cells(r, 4).Value = "ストップ" Then c = 9 End If Case "2" '修正スタート If w.Cells(r, 4).Value = "スタート" Then MsgBox serial & "-" & suffix & "は既にスタートしています。" Exit Sub End If If w.Cells(r, 4).Value = "" Or w.Cells(r, 4).Value = "ストップ" Then c = 9 End If Case "3" 'ストップ If w.Cells(r, 4).Value = "スタート" Then c = 10 End If If w.Cells(r, 4).Value = "" Or w.Cells(r, 4).Value = "ストップ" Then MsgBox serial & "-" & suffix & "はスタートしていません。" Exit Sub End If Case "4" '指定工番の完了 If w.Cells(r, 4).Value = "スタート" Then MsgBox serial & "-" & suffix & "はスタートしているため完了できません。" Exit Sub End If If w.Cells(r, 4).Value = "ストップ" Or w.Cells(r, 4).Value = "" Then w.Cells(r, 1).Value = worker w.Cells(r, 2).Value = serial w.Cells(r, 3).Value = suffix w.Cells(r, 4).Value = "完了" w.Cells(r, 4).Interior.ColorIndex = 0 Exit Sub End If End Select Do While True If w.Cells(r, c).Value = "" Then w.Cells(r, 1).Value = worker w.Cells(r, 2).Value = serial w.Cells(r, 3).Value = suffix w.Cells(r, 4).Value = state If (mark = 1 Or mark = 2) Then 'スタート w.Cells(r, 4).Interior.Color = RGB(255, 0, 0) w.Cells(r, 7).Value = "" w.Cells(r, c - 1).Value = typ End If If (mark = 3) Then 'ストップ w.Cells(r, 4).Interior.Color = RGB(255, 255, 0) w.Cells(r, 7).Value = c - 2 End If w.Cells(r, c).Value = Format(Now(), "yyyy/mm/dd hh:mm:00") Exit Do End If c = c + 3 Loop End Sub Private Sub UserForm_Initialize() On Error GoTo try UserForm1.ComboBox1.Clear 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 "select ID,worker from worker;", cn Do Until rs.EOF If rs(1) <> "" Then UserForm1.ComboBox1.AddItem rs(1) End If rs.movenext Loop If rs.state = 1 Then rs.Close Set rs = Nothing If cn.state = 1 Then cn.Close Set cn = Nothing Exit Sub try: If rs.state = 1 Then rs.Close Set rs = Nothing If cn.state = 1 Then cn.Close Set cn = Nothing MsgBox Err.Description End Sub |
Sheet5(マスタ管理)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
Function BuildQuery(mark) q_i = Sheets("マスタ管理").Range("B9").Value q_c = Sheets("マスタ管理").Range("B3").Value q_i = StrConv(q_i, vbNarrow) q_i = Replace(q_i, "'", "’") q_c = Replace(q_c, "'", "’") q_i = Replace(q_i, """", "’") q_c = Replace(q_c, """", "’") If mark = "s" Then q = "select ID,worker from worker;" If mark = "i" Then q = "insert into worker (worker) values ('" & q_c & "');" If mark = "u" Then q = "update worker set worker = '" & q_c & "' where ID = " & q_i & ";" If mark = "d" Then q = "delete from worker where ID = " & q_i & ";" BuildQuery = q End Function Private Sub CommandButton1_Click() If db_chk = False Then Exit Sub If form_chk = False Then Exit Sub If form_query("i") = False Then Exit Sub grid_clear If grid_load("s") = 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 form_query("u") = False Then Exit Sub grid_clear If grid_load("s") = 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("s") = False Then Exit Sub form_clear End Sub Function id_chk() On Error GoTo try If Sheets("マスタ管理").Range("B9").Value = "" Then GoTo try v = CLng(Sheets("マスタ管理").Range("B9").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 form_chk = True Exit Function try: MsgBox "入力が不足しています。" form_chk = False End Function Sub grid_clear() Sheets("マスタ管理").Range("G4:H100").Delete End Sub Sub form_clear() Sheets("マスタ管理").Range("B3").Value = "" Sheets("マスタ管理").Range("B9").Value = "" End Sub 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("マスタ管理").Range("G" & r & ":H" & 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 CommandButton4_Click() If db_chk = False Then Exit Sub grid_clear If grid_load("s") = False Then Exit Sub form_clear End Sub 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(9, 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(9, 2).Value = "" End If End Sub |
Sheet3(作業登録)
1 2 3 4 5 6 7 8 |
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If UserForm1.Visible And UserForm1.TextBox8.Text = "" And _ Target.Row >= 2 And Target.Column = 2 And Target.Text <> "" Then UserForm1.TextBox1.Text = Target.Text End If End Sub |
Sheet1(登録者)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
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 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 |
VBA 工数管理プログラム
前回のC#版を中止してから即席で作った。C#のときはMySQLだったが、今回はmdbファイルとした。使うのが1~2名であっても複数箇所で開く場合、データは別ファイルの方がいいだろうと判断。
今回は使う場面を想像できたので、あまりしっかり対策していないが、使う人が書式や関数を変更してしまっても大丈夫なように、起動時にしっかり設定し直す処理をいれれば、エクセルVBAでもDBのクライアントとしてかなり使えそう。
UserForm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
Function form_query() 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 "select ID,customer,model,serial,quantity,notes from orders where serial = '" & UserForm1.TextBox1.Text & "';", cn Do Until rs.EOF UserForm1.TextBox8.Text = rs(0) UserForm1.TextBox2.Text = rs(1) UserForm1.TextBox3.Text = rs(2) UserForm1.TextBox1.Text = rs(3) UserForm1.TextBox7.Text = rs(4) UserForm1.TextBox4.Text = rs(5) rs.movenext Loop If UserForm1.TextBox8.Text <> "" Then UserForm1.TextBox5.Text = "1" UserForm1.TextBox6.Text = UserForm1.TextBox7.Text End If If rs.state = 1 Then rs.Close Set rs = Nothing If cn.state = 1 Then cn.Close Set cn = Nothing form_query = True Exit Function try: If rs.state = 1 Then rs.Close Set rs = Nothing If cn.state = 1 Then cn.Close Set cn = Nothing MsgBox Err.Description form_query = False End Function Private Sub CommandButton1_Click() If UserForm1.TextBox1.Text = "" Then MsgBox "工番を入力してください。" Exit Sub End If If db_chk = False Then Exit Sub form_clear form_query End Sub Sub form_clear() UserForm1.TextBox8.Text = "" UserForm1.TextBox2.Text = "" UserForm1.TextBox3.Text = "" UserForm1.TextBox7.Text = "" UserForm1.TextBox4.Text = "" UserForm1.TextBox5.Text = "" UserForm1.TextBox6.Text = "" End Sub 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 Function qty_parse() On Error GoTo try If UserForm1.TextBox5.Text = "" Or UserForm1.TextBox6.Text = "" Then MsgBox "対象枝を入力してください。" qty_parse = False Exit Function End If qty = CInt(UserForm1.TextBox7.Text) suffix_s = CInt(UserForm1.TextBox5.Text) suffix_e = CInt(UserForm1.TextBox6.Text) If suffix_s = 0 Or suffix_e = 0 Then MsgBox "対象枝は1以上の数字を指定する必要があります。" qty_parse = False Exit Function End If If suffix_e > qty Or suffix_s > suffix_e Then MsgBox "対象枝の指定範囲に問題があります。" qty_parse = False Exit Function End If qty_parse = True Exit Function try: MsgBox "対象枝が数字ではありません。" qty_parse = False End Function Private Sub CommandButton2_Click() Call MainWrite("1", "スタート", "作") End Sub Private Sub CommandButton3_Click() Call MainWrite("1", "スタート", "修") End Sub Private Sub CommandButton4_Click() Call MainWrite("3", "ストップ", "") End Sub Private Sub CommandButton5_Click() Call MainWrite("4", "完了", "") WorkTimeAdd End Sub Sub MainWrite(mark, state, typ) If ActiveSheet.Name <> "作業登録" Then MsgBox "作業登録で実施してください。" Exit Sub End If If UserForm1.TextBox8.Text = "" Then MsgBox "対象のIDが見つかりません。" Exit Sub End If If qty_parse = False Then Exit Sub Set w = Sheets("作業登録") serial = UserForm1.TextBox1.Text For suffix = CInt(UserForm1.TextBox5.Text) To CInt(UserForm1.TextBox6.Text) f = False For r = 2 To w.UsedRange.Rows.Count If serial = w.Cells(r, 1).Text And suffix = w.Cells(r, 2) Then f = True Call WriteCol(r, serial, suffix, mark, state, typ) End If Next r If f = False Then Call WriteCol(r, serial, suffix, mark, state, typ) End If Next suffix End Sub Sub WriteCol(r, serial, suffix, mark, state, typ) Set w = Sheets("作業登録") If w.Cells(r, 3).Value = "完了" Then MsgBox serial & "-" & suffix & "は既に完了しています。" Exit Sub End If Select Case mark Case "1" '作業スタート If w.Cells(r, 3).Value = "スタート" Then MsgBox serial & "-" & suffix & "は既にスタートしています。" Exit Sub End If If w.Cells(r, 3).Value = "" Or w.Cells(r, 3).Value = "ストップ" Then c = 8 End If Case "2" '修正スタート If w.Cells(r, 3).Value = "スタート" Then MsgBox serial & "-" & suffix & "は既にスタートしています。" Exit Sub End If If w.Cells(r, 3).Value = "" Or w.Cells(r, 3).Value = "ストップ" Then c = 8 End If Case "3" 'ストップ If w.Cells(r, 3).Value = "スタート" Then c = 9 End If If w.Cells(r, 3).Value = "" Or w.Cells(r, 3).Value = "ストップ" Then MsgBox serial & "-" & suffix & "はスタートしていません。" Exit Sub End If Case "4" '指定工番の完了 If w.Cells(r, 3).Value = "スタート" Then MsgBox serial & "-" & suffix & "はスタートしているため完了できません。" Exit Sub End If If w.Cells(r, 3).Value = "ストップ" Or w.Cells(r, 3).Value = "" Then w.Cells(r, 1).Value = serial w.Cells(r, 2).Value = suffix w.Cells(r, 3).Value = "完了" w.Cells(r, 3).Interior.ColorIndex = 0 Exit Sub End If End Select Do While True If w.Cells(r, c).Value = "" Then w.Cells(r, 1).Value = serial w.Cells(r, 2).Value = suffix w.Cells(r, 3).Value = state If (mark = 1 Or mark = 2) Then 'スタート w.Cells(r, 3).Interior.Color = RGB(255, 0, 0) w.Cells(r, 6).Value = "" w.Cells(r, c - 1).Value = typ End If If (mark = 3) Then 'ストップ w.Cells(r, 3).Interior.Color = RGB(255, 255, 0) w.Cells(r, 6).Value = c - 2 End If w.Cells(r, c).Value = Format(Now(), "yyyy/mm/dd hh:mm:00") Exit Do End If c = c + 3 Loop End Sub Private Sub CommandButton6_Click() UserForm1.TextBox1.Text = "" form_clear End Sub |
Module1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
Sub auto_open() SetStyle If (StrConv(Sheets("設定").Range("B5").Value, 10) = "on") Then Sheets("作業登録").Activate UserForm1.Show End If On Error Resume Next Application.CommandBars("cell").Controls("フォーム表示").Delete Application.CommandBars("cell").Controls("フォーム表示").Delete With Application.CommandBars("cell").Controls.Add .Caption = "フォーム表示" .OnAction = "FormShow" End With End Sub Sub auto_close() On Error Resume Next Application.CommandBars("cell").Controls("フォーム表示").Delete Application.CommandBars("cell").Controls("フォーム表示").Delete End Sub Sub FormShow() Sheets("作業登録").Activate UserForm1.Show End Sub Sub SetStyle() Sheets("登録者").Cells(3, 2).NumberFormatLocal = "@" Sheets("登録者").Cells(5, 2).NumberFormatLocal = "@" Sheets("登録者").Cells(7, 2).NumberFormatLocal = "@" Sheets("登録者").Cells(11, 2).NumberFormatLocal = "@" Sheets("作業登録").Cells.NumberFormatLocal = "" Sheets("作業登録").Columns("A").NumberFormatLocal = "@" For c = 7 To 100 Step 3 Sheets("作業登録").Cells(1, c).Value = "種" Sheets("作業登録").Cells(1, c + 1).Value = "スタート" Sheets("作業登録").Cells(1, c + 2).Value = "ストップ" Sheets("作業登録").Columns(c).NumberFormatLocal = "" Sheets("作業登録").Columns(c + 1).NumberFormatLocal = "mm/dd hh:mm" Sheets("作業登録").Columns(c + 2).NumberFormatLocal = "mm/dd hh:mm" Sheets("作業登録").Columns(c).ColumnWidth = "2.5" Sheets("作業登録").Columns(c + 1).ColumnWidth = "12" Sheets("作業登録").Columns(c + 2).ColumnWidth = "12" Next c End Sub Sub WorkTimeAdd() Set w = Sheets("作業登録") For r = 2 To w.UsedRange.Rows.Count w.Cells(r, 4) = "" w.Cells(r, 5) = "" If w.Cells(r, 3).Text = "完了" And w.Cells(r, 6).Text <> "" Then n = 0 f = 0 For c = 7 To w.Cells(r, 6).Text Step 3 Select Case w.Cells(r, c).Text Case "作" n = n + WorkTime(w.Cells(r, c + 1).Text, w.Cells(r, c + 2).Text) Case "修" f = f + WorkTime(w.Cells(r, c + 1).Text, w.Cells(r, c + 2).Text) End Select Next c w.Cells(r, 4) = Int(n / 60) & "." & Format(n Mod 60, "00") & " H" w.Cells(r, 5) = Int(f / 60) & "." & Format(f Mod 60, "00") & " H" End If Next r w.Activate End Sub Function WorkTime(date_s, date_e) On Error GoTo try date_s = CDate(date_s) date_e = CDate(date_e) ts = 0 Do While date_s < date_e ts = ts + 1 If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 10:00:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 10:15:00") Then ts = ts - 1 End If If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 12:00:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 12:50:00") Then ts = ts - 1 End If If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 15:00:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 15:15:00") Then ts = ts - 1 End If If date_s >= CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 17:20:00") And _ date_s < CDate(Year(date_s) & "/" & Month(date_s) & "/" & Day(date_s) & " 17:30:00") Then ts = ts - 1 End If date_s = DateAdd("n", 1, date_s) Loop WorkTime = ts Exit Function try: WorkTime = 0 End Function |
Sheet1(登録者)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
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 |
Sheet3(作業登録)
1 2 3 4 5 6 7 8 |
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If UserForm1.Visible And UserForm1.TextBox8.Text = "" And _ Target.Row >= 2 And Target.Column = 1 And Target.Text <> "" Then UserForm1.TextBox1.Text = Target.Text End If End Sub |
C# 工数管理プログラム
テストで作ってみたが方向性変更のため未使用。複数のPCからバーコードを使い製作時間(工数)を登録するプログラム。
今後、時間のあるとき稼働時間算出の設定について変更できるようにしようかなと。
MySQLが必要なので、XAMPPを使うのが一番簡単で、XAMPPのシェルからMySQLにログインしたら、以下をコピペで実行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
CREATE DATABASE man_hours CHARSET=sjis; USE man_hours; CREATE TABLE items ( items_id int unsigned NOT NULL AUTO_INCREMENT, orders_id int DEFAULT NULL, suffix_no int DEFAULT NULL, PRIMARY KEY (items_id) ) DEFAULT CHARSET=sjis; CREATE TABLE orders ( orders_id int unsigned NOT NULL AUTO_INCREMENT, customer varchar(100) DEFAULT NULL, model varchar(100) DEFAULT NULL, serial_no varchar(100) DEFAULT NULL, quantity smallint unsigned DEFAULT NULL, notes varchar(100) DEFAULT NULL, visible varchar(2) DEFAULT NULL, PRIMARY KEY (orders_id) ) DEFAULT CHARSET=sjis; CREATE TABLE tasks_e ( tasks_id int unsigned NOT NULL AUTO_INCREMENT, items_id int DEFAULT NULL, workers_id int DEFAULT NULL, end_dt datetime DEFAULT NULL, PRIMARY KEY (tasks_id), UNIQUE KEY (items_id,workers_id) ) DEFAULT CHARSET=sjis; CREATE TABLE tasks_p ( tasks_id int unsigned NOT NULL AUTO_INCREMENT, items_id int DEFAULT NULL, workers_id int DEFAULT NULL, adjust varchar(10) DEFAULT NULL, PRIMARY KEY (tasks_id), UNIQUE KEY (items_id,workers_id) ) DEFAULT CHARSET=sjis; CREATE TABLE tasks_s ( tasks_id int unsigned NOT NULL AUTO_INCREMENT, items_id int DEFAULT NULL, workers_id int DEFAULT NULL, start_dt datetime DEFAULT NULL, PRIMARY KEY (tasks_id), UNIQUE KEY (items_id,workers_id) ) DEFAULT CHARSET=sjis; CREATE TABLE workers ( workers_id int unsigned NOT NULL AUTO_INCREMENT, worker_name varchar(100) DEFAULT NULL, work_type varchar(100) DEFAULT NULL, PRIMARY KEY (workers_id) ) DEFAULT CHARSET=sjis; |
起動後以下の画面が表示されるので、接続先はIPアドレス、DB名はman_hours、ユーザー、パスワードはMySQL作成時に指定したユーザー、パスワード。
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form1 : Form { public Form1() { Form9 f = new Form9(); f.ShowDialog(); Db.DbHost = f.DbHost; Db.DbName = f.DbName; Db.DbUser = f.DbUser; Db.DbPass = f.DbPass; InitializeComponent(); ImeMode = ImeMode.Off; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); foreach (Control c in Controls) { c.Enabled = false; } return; } } private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(); f.ShowDialog(); } private void button3_Click(object sender, EventArgs e) { Form3 f = new Form3(); f.ShowDialog(); } private void button2_Click(object sender, EventArgs e) { Form6 f = new Form6(); f.ShowDialog(); } private void button4_Click(object sender, EventArgs e) { Form7 f = new Form7(); f.ShowDialog(); } } } |
Form2.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ZXing; namespace 製品工数管理 { public partial class Form2 : Form { string default_query = "select orders_id,customer,model,serial_no,quantity,notes,visible,'表示' from orders where visible = '未'"; public Form2() { InitializeComponent(); ImeMode = ImeMode.Off; dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.DefaultCellStyle.Font = new Font("Meiryo UI", 11); Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } foreach(string s in d.LoadCustomer()) { comboBox2.Items.Add(s); } foreach (string s in d.LoadModel()) { comboBox3.Items.Add(s); } d.ExecuteReader(default_query, "", dataGridView1); } public void CreateBarcode(string code) { BarcodeWriter br = new BarcodeWriter(); br.Format = BarcodeFormat.CODE_128; br.Options.Height = 79; br.Options.Width = 151; br.Options.Margin = 10; br.Options.PureBarcode = false; Bitmap b = br.Write(code); pictureBox1.BackgroundImage = b; } private Boolean CheckTextBox() { if (comboBox2.Text == "" || comboBox3.Text == "" || textBox3.Text == "" || textBox4.Text == "") { MessageBox.Show("備考以外は入力必須です。"); return false; } try { int.Parse(textBox4.Text); } catch { MessageBox.Show("数量が正しくありません。"); return false; } comboBox2.Text = comboBox2.Text.Trim(); comboBox3.Text = comboBox3.Text.Trim(); textBox3.Text = textBox3.Text.Trim(); textBox6.Text = textBox6.Text.Trim(); comboBox2.Text = comboBox2.Text.Replace("\'", "’"); comboBox3.Text = comboBox3.Text.Replace("\'", "’"); textBox3.Text = textBox3.Text.Replace("\'", "’"); textBox6.Text = textBox6.Text.Replace("\'", "’"); comboBox2.Text = comboBox2.Text.Replace("\"", "”"); comboBox3.Text = comboBox3.Text.Replace("\"", "”"); textBox3.Text = textBox3.Text.Replace("\"", "”"); textBox6.Text = textBox6.Text.Replace("\"", "”"); return true; } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (r == -1 || c == -1) return; textBox5.Text = dataGridView1.Rows[r].Cells[0].Value.ToString(); comboBox2.Text = dataGridView1.Rows[r].Cells[1].Value.ToString(); comboBox3.Text = dataGridView1.Rows[r].Cells[2].Value.ToString(); textBox3.Text = dataGridView1.Rows[r].Cells[3].Value.ToString(); textBox4.Text = dataGridView1.Rows[r].Cells[4].Value.ToString(); textBox6.Text = dataGridView1.Rows[r].Cells[5].Value.ToString(); comboBox1.Text = dataGridView1.Rows[r].Cells[6].Value.ToString(); CreateBarcode(textBox5.Text); if (c == 7) { Form4 f = new Form4(textBox5.Text, comboBox2.Text, comboBox3.Text, textBox3.Text, textBox4.Text, textBox6.Text); f.ShowDialog(); } } private void button1_Click(object sender, EventArgs e) { if (!CheckTextBox()) return; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } string q1 = @"insert into orders (customer,model,serial_no,quantity,notes,visible) " + "values ('" + comboBox2.Text + "','" + comboBox3.Text + "','" + textBox3.Text + "'," + textBox4.Text + ",'" + textBox6.Text + "','未');"; string lastId = d.ExecuteNonReader(q1, ""); if(lastId == "") { MessageBox.Show("枝番登録に問題があります。オーダーを再登録してください。"); return; } else { string q2 = "insert into items (orders_id,suffix_no) values "; for (int i = 1; i <= int.Parse(textBox4.Text); i++) { q2 += "(" + lastId + "," + i.ToString() + ")"; if (i == int.Parse(textBox4.Text)) { q2 += ";"; } else { q2 += ","; } } d.ExecuteNonReader(q2, ""); } d.ExecuteReader(default_query, "", dataGridView1); button2.PerformClick(); MessageBox.Show("登録しました。"); } private void button2_Click(object sender, EventArgs e) { comboBox2.Text = ""; comboBox3.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; comboBox1.Text = "未"; pictureBox1.BackgroundImage = null; } private void button3_Click(object sender, EventArgs e)//更新 { if (textBox5.Text == "") { MessageBox.Show("変更対象を選択してください。"); return; } if (!CheckTextBox()) return; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } //数量更新不可 if (!d.QuantityCheck("select quantity from orders where orders_id = " + textBox5.Text + ";", textBox4.Text)) { MessageBox.Show("数量は更新できません。"); return; } string q = "update orders set customer = '" + comboBox2.Text + "', model = '" + comboBox3.Text + "' , serial_no = '" + textBox3.Text + "' , quantity = " + textBox4.Text + " , notes = '" + textBox6.Text + "' , visible = '" + comboBox1.Text + "' where orders_id = " + textBox5.Text + ";"; d.ExecuteNonReader(q, ""); d.ExecuteReader(default_query, "", dataGridView1); button2.PerformClick(); MessageBox.Show("更新しました。"); } private void button4_Click(object sender, EventArgs e)//削除 { DialogResult yn = MessageBox.Show("本当に削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; if (textBox5.Text == "") { MessageBox.Show("削除対象を選択してください。"); return; } if (!CheckTextBox()) return; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } string q = "delete from orders where orders_id = " + textBox5.Text + ";"; d.ExecuteNonReader(q, ""); q = "delete from items where orders_id = " + textBox5.Text + ";"; d.ExecuteNonReader(q, ""); d.ExecuteReader(default_query, "", dataGridView1); button2.PerformClick(); MessageBox.Show("削除しました。"); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } if (checkBox1.Checked) { default_query = "select orders_id,customer,model,serial_no,quantity,notes,visible,'表示' from orders where visible = '完'"; } else { default_query = "select orders_id,customer,model,serial_no,quantity,notes,visible,'表示' from orders where visible = '未'"; } d.ExecuteReader(default_query, "", dataGridView1); button2.PerformClick(); } } } |
Form3.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace 製品工数管理 { public partial class Form3 : Form { public Form3() { InitializeComponent(); ImeMode = ImeMode.Off; dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.DefaultCellStyle.Font = new Font("Meiryo UI", 11); Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } d.ExecuteReader("select * from workers;","",dataGridView1); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (r == -1 || c == -1) return; textBox1.Text = dataGridView1.Rows[r].Cells[0].Value.ToString(); textBox2.Text = dataGridView1.Rows[r].Cells[1].Value.ToString(); textBox3.Text = dataGridView1.Rows[r].Cells[2].Value.ToString(); } private void button4_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; } private void button2_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("変更対象を選択してください。"); return; } if (!CheckTextBox()) return; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } string q = @"update workers set worker_name = '" + textBox2.Text + "', work_type = '" + textBox3.Text + "' where workers_id = " + textBox1.Text + ";"; d.ExecuteNonReader(q, ""); d.ExecuteReader("select * from workers;", "", dataGridView1); button4.PerformClick(); MessageBox.Show("更新しました"); } private void button3_Click(object sender, EventArgs e) { DialogResult yn = MessageBox.Show("本当に削除しますか?","",MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; if (textBox1.Text == "") { MessageBox.Show("削除対象を選択してください。"); return; } if (!CheckTextBox()) return; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } string q = @"delete from workers where workers_id = " + textBox1.Text + ";"; d.ExecuteNonReader(q, ""); d.ExecuteReader("select * from workers;", "", dataGridView1); button4.PerformClick(); MessageBox.Show("削除しました。"); } private void button1_Click(object sender, EventArgs e) { if (!CheckTextBox()) return; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } string q = @"insert into workers (worker_name,work_type) values ('" + textBox2.Text + "','" + textBox3.Text + "');"; d.ExecuteNonReader(q, ""); d.ExecuteReader("select * from workers;", "", dataGridView1); button4.PerformClick(); MessageBox.Show("登録しました。"); } private Boolean CheckTextBox() { if (textBox2.Text == "" || textBox3.Text == "") { MessageBox.Show("全て入力必須です。"); return false; } textBox2.Text = textBox2.Text.Trim(); textBox3.Text = textBox3.Text.Trim(); textBox2.Text = textBox2.Text.Replace("\'", "’"); textBox3.Text = textBox3.Text.Replace("\'", "’"); textBox2.Text = textBox2.Text.Replace("\"", "”"); textBox3.Text = textBox3.Text.Replace("\"", "”"); return true; } } } |
Form4.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ZXing; namespace 製品工数管理 { public partial class Form4 : Form { public Form4(string orders_id, string customer, string model, string serial_no, string quantity, string notes) { InitializeComponent(); ImeMode = ImeMode.Off; textBox5.Text = orders_id; textBox1.Text = customer; textBox2.Text = model; textBox3.Text = serial_no; textBox4.Text = quantity; textBox6.Text = notes; CreateBarcode(orders_id); } public void CreateBarcode(string code) { BarcodeWriter br = new BarcodeWriter(); br.Format = BarcodeFormat.CODE_128; br.Options.Height = 80; br.Options.Width = 245; br.Options.Margin = 10; br.Options.PureBarcode = false; Bitmap b = br.Write(code); pictureBox1.BackgroundImage = b; } public void PanelPrint() { System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler((object sender, System.Drawing.Printing.PrintPageEventArgs e) => { Bitmap bmp = new Bitmap(panel1.Width, panel1.Height); panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height)); e.Graphics.DrawImage(bmp, e.MarginBounds); e.HasMorePages = false; bmp.Dispose(); }); PrintDialog pdg = new PrintDialog(); pdg.Document = pd; if (pdg.ShowDialog() == DialogResult.OK) pd.Print(); } private void button1_Click(object sender, EventArgs e) { PanelPrint(); } private void button2_Click(object sender, EventArgs e) { SaveFileDialog sd = new SaveFileDialog(); sd.FileName = "orders.png"; sd.InitialDirectory = @"c:\"; sd.Title = "保存先を指定してください。"; if(sd.ShowDialog() == DialogResult.OK) { Bitmap bmp = new Bitmap(panel1.Width, panel1.Height); panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height)); bmp.Save(sd.FileName); bmp.Dispose(); } } } } |
Form5.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form5 : Form { public Form5(string worker_name, string wokers_id, string orders_id) { InitializeComponent(); ImeMode = ImeMode.Off; timer1.Start(); dataGridView1.AllowUserToAddRows = false; dataGridView1.Columns[0].ReadOnly = true; dataGridView1.Columns[1].ReadOnly = true; dataGridView1.Columns[2].ReadOnly = true;//枝番 dataGridView1.Columns[3].ReadOnly = true;//開始 dataGridView1.Columns[4].ReadOnly = true;//終了 dataGridView1.Columns[5].ReadOnly = true;//調整 dataGridView1.Columns[6].ReadOnly = true;//稼働 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.Columns[2].Width = 50; dataGridView1.Columns[3].Width = 200; dataGridView1.Columns[4].Width = 200; dataGridView1.Columns[5].Width = 110; dataGridView1.Columns[6].Width = 110; dataGridView1.Columns[7].Width = 50; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.DefaultCellStyle.Font = new Font("Meiryo UI", 11); dataGridView1.Columns[3].DefaultCellStyle.Font = new Font("Meiryo UI", 16); dataGridView1.Columns[4].DefaultCellStyle.Font = new Font("Meiryo UI", 16); dataGridView1.Columns[5].DefaultCellStyle.Font = new Font("Meiryo UI", 16); dataGridView1.Columns[6].DefaultCellStyle.Font = new Font("Meiryo UI", 16); textBox7.Text = wokers_id; label10.Text = "" + worker_name + " さんの入力画面です。"; label10.Tag = worker_name; comboBox1.Items.AddRange(new string[]{ "00:15" , "00:30" , "01:00" , "02:00" , "04:00" , "08:00"}); comboBox1.Text = "00:15"; //作業中一覧から起動 if(orders_id != "") { textBox5.Text = orders_id; button1.PerformClick(); } } private void timer1_Tick(object sender, EventArgs e) { label7.Text = DateTime.Now.ToString("yyyy/MM/dd (dddd)"); label8.Text = DateTime.Now.ToString("HH:mm:ss"); } public string DefaultQuery() { return "select items.orders_id , items.items_id , items.suffix_no , " + "date_format(tasks_s.start_dt, '%y-%m-%d %H:%i') , " + "date_format(tasks_e.end_dt, '%y-%m-%d %H:%i') , " + "substring(tasks_p.adjust,1,length(tasks_p.adjust)-3) ,'','False' " + "from items left outer join tasks_s on items.items_id = tasks_s.items_id and tasks_s.workers_id = " + textBox7.Text + " " + "left outer join tasks_e on items.items_id = tasks_e.items_id and tasks_e.workers_id = " + textBox7.Text + " " + "left outer join tasks_p on items.items_id = tasks_p.items_id and tasks_p.workers_id = " + textBox7.Text + " " + "where items.orders_id = " + textBox5.Text + " order by items.items_id;"; //items.items_id は primary key。tasks_x.items_id と tasks_x.workers_id で 複合unique key。1対1のjoin。 } private void button1_Click(object sender, EventArgs e) { if (textBox5.Text == "") { MessageBox.Show("オーダーIDが入力されていません。"); return; } Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } List<TextBox> t = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox6 }; if (!d.FormLoadExecuteReader("select customer,model,serial_no,quantity,notes from orders where orders_id = " + textBox5.Text + ";", t)) { MessageBox.Show("オーダーIDが存在しません。"); return; } d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; } private void SetWorkTime() { for (int r = 0; r < dataGridView1.Rows.Count; r++) { string s = dataGridView1.Rows[r].Cells[3].Value.ToString(); string e = dataGridView1.Rows[r].Cells[4].Value.ToString(); string a = dataGridView1.Rows[r].Cells[5].Value.ToString(); if (s != "" && e != "" && a != "") { dataGridView1.Rows[r].Cells[6].Value = Db.WorkTime(DateTime.Parse(s), DateTime.Parse(e), TimeSpan.Parse(a)); } if (s != "" && e != "" && a == "") { dataGridView1.Rows[r].Cells[6].Value = Db.WorkTime(DateTime.Parse(s), DateTime.Parse(e), new TimeSpan(0,0,0)); } if (s == "" || e == "") { dataGridView1.Rows[r].Cells[6].Value = "-"; } } } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (c == 3 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "")//開始 { Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } Form10 f = new Form10(); f.ShowDialog(); if (f.getDatetime == "") { DialogResult yn = MessageBox.Show("削除しますか?(終了も削除されます)", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string workers_id = textBox7.Text; d.ExecuteNonReader("delete from tasks_s where items_id = " + items_id + " and workers_id = " + workers_id + ";", ""); d.ExecuteNonReader("delete from tasks_e where items_id = " + items_id + " and workers_id = " + workers_id + ";", ""); d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("削除しました。"); } else { string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string workers_id = textBox7.Text; d.ExecuteNonReader("update tasks_s set start_dt = '" + f.getDatetime + "' where items_id = " + items_id + " and workers_id = " + workers_id + ";", ""); d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("更新しました。"); } } if (c == 4 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "")//終了 { Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } Form10 f = new Form10(); f.ShowDialog(); if (f.getDatetime == "") { DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string workers_id = textBox7.Text; d.ExecuteNonReader("delete from tasks_e where items_id = " + items_id + " and workers_id = " + workers_id + ";", ""); d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("削除しました。"); } else { string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string workers_id = textBox7.Text; d.ExecuteNonReader("update tasks_e set end_dt = '" + f.getDatetime + "' where items_id = " + items_id + " and workers_id = " + workers_id + ";", ""); d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("更新しました。"); } } if (c == 5 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "")//調整 { DialogResult yn = MessageBox.Show("削除しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string workers_id = textBox7.Text; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } d.ExecuteNonReader("delete from tasks_p where items_id = " + items_id + " and workers_id = " + workers_id + ";", ""); d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("削除しました。"); } } private void OnOff(object sender, EventArgs e) { if (button2 == sender) InsertDatetime("開始", 3, "tasks_s", "start_dt"); if (button3 == sender) InsertDatetime("終了", 4, "tasks_e", "end_dt"); } public void InsertDatetime(string cap, int col_no, string tbl,string col_name) { if (dataGridView1.Rows.Count == 0) return; for (int r = 0; r < dataGridView1.Rows.Count; r++) { string check = dataGridView1.Rows[r].Cells[7].Value.ToString(); if (check == "True" && dataGridView1.Rows[r].Cells[col_no].Value.ToString() != "") { MessageBox.Show("既に" + cap + "時刻が登録されています。削除後に登録してください。"); return; } if (check == "True" && col_no == 4 && dataGridView1.Rows[r].Cells[3].Value.ToString() == "") { MessageBox.Show("作業開始の前に作業終了を登録できません。"); return; } } string query = "insert into " + tbl + " (items_id,workers_id," + col_name + ") values "; Boolean queryExists = false; for (int r = 0; r < dataGridView1.Rows.Count; r++) { string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string check = dataGridView1.Rows[r].Cells[7].Value.ToString(); if (check == "True" && dataGridView1.Rows[r].Cells[col_no].Value.ToString() == "") { query += "(" + items_id + "," + textBox7.Text + ",'" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "'),"; queryExists = true; } } if (queryExists) { query = query.Substring(0, query.Length - 1) + ";"; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } d.ExecuteNonReader(query, ""); d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("登録しました。"); } } private void UpDown(object sender, EventArgs e) { if (dataGridView1.Rows.Count == 0) return; Boolean f = false; for (int r = 0; r < dataGridView1.Rows.Count; r++) { if ("True" == dataGridView1.Rows[r].Cells[7].Value.ToString()) f = true; } if (f && (Button)sender == button4) PauseClick(TimeSpan.Parse("" + comboBox1.Text)); if (f && (Button)sender == button5) PauseClick(TimeSpan.Parse("-" + comboBox1.Text)); } public void PauseClick(TimeSpan t) { Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } for (int r = 0; r < dataGridView1.Rows.Count; r++) { string items_id = dataGridView1.Rows[r].Cells[1].Value.ToString(); string check = dataGridView1.Rows[r].Cells[7].Value.ToString(); if (check == "True") { string adjust_col = dataGridView1.Rows[r].Cells[5].Value.ToString(); TimeSpan ts ; if(adjust_col == "") { ts = t; } else { ts = t + TimeSpan.Parse(adjust_col); } string q = "delete from tasks_p where items_id = " + items_id + " and workers_id = " + textBox7.Text + ";"; d.ExecuteNonReader(q, ""); if (ts != TimeSpan.Zero) { q = "insert into tasks_p (items_id,workers_id,adjust) values (" + items_id + "," + textBox7.Text + ",'" + ts.ToString() + "');"; d.ExecuteNonReader(q, ""); } } } d.ExecuteReader(DefaultQuery(), "", dataGridView1); SetWorkTime(); checkBox1.Checked = false; MessageBox.Show("登録しました。"); } private void button6_Click(object sender, EventArgs e) { Form8 f = new Form8(textBox7.Text,label10.Tag.ToString()); f.ShowDialog(); if(f.ReturnOrders_id != "") { textBox5.Text = f.ReturnOrders_id; button1.PerformClick(); } } private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (dataGridView1.Rows.Count == 0) return; for (int r = 0; r < dataGridView1.Rows.Count; r++) { dataGridView1.Rows[r].Cells[7].Value = checkBox1.Checked.ToString(); } } } } |
Form6.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form6 : Form { public Form6() { InitializeComponent(); ImeMode = ImeMode.Off; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } string[][] workers = d.LoadWorkers(); int count = workers.Length; int pages = 1; tabControl1.ItemSize = new Size(1, 55); if(count <= 24) { tabControl1.TabPages.Add("1ページ"); } else if(count > 24 && count <= 48) { tabControl1.TabPages.Add("1ページ"); tabControl1.TabPages.Add("2ページ"); pages = 2; } else if (count > 48) { tabControl1.TabPages.Add("1ページ"); tabControl1.TabPages.Add("2ページ"); tabControl1.TabPages.Add("3ページ"); pages = 3; } int i = 0; for (int p = 0; p < pages; p++) { int x = 6; int y = 6; for (int r = 0; r < 6; r++) { for (int c = 0; c < 4; c++) { if (i == count) break; Button b = new Button(); b.Font = new Font("Meiryo UI", 16); b.Text = workers[i][1].ToString(); b.Tag = workers[i][0].ToString(); b.Size = new Size(206, 71); b.Location = new Point(y + c * (206 + 6), x + r * (71 + 6)); b.Click += new EventHandler(ClickWorker); tabControl1.TabPages[p].Controls.Add(b); i++; } } } } public void ClickWorker(object sender, EventArgs e) { Form5 f = new Form5(((Button)sender).Text, ((Button)sender).Tag.ToString(),""); f.ShowDialog(); } } } |
Form7.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form7 : Form { public Form7() { InitializeComponent(); ImeMode = ImeMode.Off; string[] col = new string[] { "オーダーID", "得意先", "型式", "工番", "枝番", "数量" , "名前" , "開始日時" , "終了日時" , "調整時間" , "稼働時間"}; for (int i = 0; i < col.Length; i++) { dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()); dataGridView1.Columns[i].HeaderText = col[i]; } dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.DefaultCellStyle.Font = new Font("Meiryo UI", 11); DbLoad(); } public string QueryWhere() { string orders_id = textBox5.Text; string customer = comboBox2.Text; string model = comboBox1.Text; string serial_no = textBox6.Text; string worker_name = comboBox3.Text; string start_dt_1 = textBox1.Text; string start_dt_2 = textBox2.Text; string end_dt_1 = textBox3.Text; string end_dt_2 = textBox4.Text; string q = ""; if (orders_id != "") { q = "where items.orders_id like '%" + orders_id + "%' "; } if (customer != "") { if (q == "") { q = "where customer like '%" + customer + "%' "; } else { q += "and customer like '%" + customer + "%' "; } } if (model != "") { if (q == "") { q = "where model like '%" + model + "%' "; } else { q += "and model like '%" + model + "%' "; } } if (serial_no != "") { if (q == "") { q = "where serial_no like '%" + serial_no + "%' "; } else { q += "and serial_no like '%" + serial_no + "%' "; } } if (worker_name != "") { if (q == "") { q = "where worker_name like '%" + worker_name + "%' "; } else { q += "and worker_name like '%" + worker_name + "%' "; } } if (start_dt_1 != "" && start_dt_2 != "") { string f = DateTime.Parse(start_dt_1).ToString("yyyy-MM-dd"); string t = DateTime.Parse(start_dt_2).AddDays(1).ToString("yyyy-MM-dd"); if (q == "") { q = "where tasks_s.start_dt >= '" + f + "' and tasks_s.start_dt < '" + t + "' "; } else { q += "and tasks_s.start_dt >= '" + f + "' and tasks_s.start_dt < '" + t + "' "; } } if (start_dt_1 != "" && start_dt_2 == "") { string f = DateTime.Parse(start_dt_1).ToString("yyyy-MM-dd"); if (q == "") { q = "where tasks_s.start_dt >= '" + f + "' "; } else { q += "and tasks_s.start_dt >= '" + f + "' "; } } if (start_dt_1 == "" && start_dt_2 != "") { string t = DateTime.Parse(start_dt_2).AddDays(1).ToString("yyyy-MM-dd"); if (q == "") { q = "where tasks_s.start_dt < '" + t + "' "; } else { q += "and tasks_s.start_dt < '" + t + "' "; } } if (end_dt_1 != "" && end_dt_2 != "") { string f = DateTime.Parse(end_dt_1).ToString("yyyy-MM-dd"); string t = DateTime.Parse(end_dt_2).AddDays(1).ToString("yyyy-MM-dd"); if (q == "") { q = "where tasks_e.end_dt >= '" + f + "' and tasks_e.end_dt < '" + t + "' "; } else { q += "and tasks_e.end_dt >= '" + f + "' and tasks_e.end_dt < '" + t + "' "; } } if (end_dt_1 != "" && end_dt_2 == "") { string f = DateTime.Parse(end_dt_1).ToString("yyyy-MM-dd"); if (q == "") { q = "where tasks_e.end_dt >= '" + f + "' "; } else { q += "and tasks_e.end_dt >= '" + f + "' "; } } if (end_dt_1 == "" && end_dt_2 != "") { string t = DateTime.Parse(end_dt_2).AddDays(1).ToString("yyyy-MM-dd"); if (q == "") { q = "where tasks_e.end_dt < '" + t + "' "; } else { q += "and tasks_e.end_dt < '" + t + "' "; } } if (q == "") { q = "where visible = '" + comboBox4.Text + "' and tasks_s.tasks_id is not null "; } else { q += "and visible = '" + comboBox4.Text + "' and tasks_s.tasks_id is not null "; } return q; } private void DbLoad() { string start_dt_1 = textBox1.Text; string start_dt_2 = textBox2.Text; string end_dt_1 = textBox3.Text; string end_dt_2 = textBox4.Text; try { if (start_dt_1 != "") DateTime.Parse(start_dt_1); if (start_dt_2 != "") DateTime.Parse(start_dt_2); if (end_dt_1 != "") DateTime.Parse(end_dt_1); if (end_dt_2 != "") DateTime.Parse(end_dt_2); } catch { MessageBox.Show("日付が正しくありません。"); return; } string q = "select items.orders_id , orders.customer , orders.model , orders.serial_no , items.suffix_no , orders.quantity , workers.worker_name , " + "date_format(tasks_s.start_dt, '%y-%m-%d %H:%i') , " + "date_format(tasks_e.end_dt, '%y-%m-%d %H:%i') , " + "substring(tasks_p.adjust,1,length(tasks_p.adjust)-3) , '' " + "from items " + "left outer join tasks_s on items.items_id = tasks_s.items_id " + "left outer join tasks_e on items.items_id = tasks_e.items_id and tasks_s.workers_id = tasks_e.workers_id " + "left outer join tasks_p on items.items_id = tasks_p.items_id and tasks_s.workers_id = tasks_p.workers_id " + "left outer join workers on tasks_s.workers_id = workers.workers_id " + "left outer join orders on items.orders_id = orders.orders_id " + QueryWhere() + " order by items.orders_id , workers.worker_name , items.suffix_no;"; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } comboBox2.Items.Clear(); comboBox1.Items.Clear(); comboBox3.Items.Clear(); foreach (string s in d.LoadCustomer()) { comboBox2.Items.Add(s); } foreach (string s in d.LoadModel()) { comboBox1.Items.Add(s); } foreach (string[] s in d.LoadWorkers()) { comboBox3.Items.Add(s[1]); } d.ExecuteReader(q,"",dataGridView1); SetWorkTime(); } private void SetWorkTime() { for (int r = 0; r < dataGridView1.Rows.Count; r++) { string s = dataGridView1.Rows[r].Cells[7].Value.ToString(); string e = dataGridView1.Rows[r].Cells[8].Value.ToString(); string a = dataGridView1.Rows[r].Cells[9].Value.ToString(); if (s != "" && e != "" && a != "") { dataGridView1.Rows[r].Cells[10].Value = Db.WorkTime(DateTime.Parse(s), DateTime.Parse(e), TimeSpan.Parse(a)); } if (s != "" && e != "" && a == "") { dataGridView1.Rows[r].Cells[10].Value = Db.WorkTime(DateTime.Parse(s), DateTime.Parse(e), new TimeSpan(0, 0, 0)); } if (s == "" || e == "") { dataGridView1.Rows[r].Cells[10].Value = "-"; } } } private void button1_Click(object sender, EventArgs e) { DbLoad(); } } } |
Form8.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form8 : Form { public string ReturnOrders_id = ""; public Form8(string workers_id,string worker_name) { InitializeComponent(); ImeMode = ImeMode.Off; string[] col = new string[] { "名前" , "オーダーID" , "得意先", "型式", "工番", "枝番", "数量", "開始日時" }; for (int i = 0; i < col.Length; i++) { dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()); dataGridView1.Columns[i].HeaderText = col[i]; } dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.DefaultCellStyle.Font = new Font("Meiryo UI", 11); string q = "select '" + worker_name + "' , items.orders_id , orders.customer , orders.model , orders.serial_no , items.suffix_no , orders.quantity , " + "date_format(tasks_s.start_dt, '%y-%m-%d %H:%i') " + "from items " + "left outer join tasks_s on items.items_id = tasks_s.items_id and tasks_s.workers_id = " + workers_id + " " + "left outer join tasks_e on items.items_id = tasks_e.items_id and tasks_e.workers_id = " + workers_id + " " + "left outer join tasks_p on items.items_id = tasks_p.items_id and tasks_p.workers_id = " + workers_id + " " + "left outer join orders on items.orders_id = orders.orders_id " + "where tasks_s.start_dt is not null and tasks_e.end_dt is null and visible = '未';"; Db d = new Db(); if (!d.DbState) { MessageBox.Show(d.DbStateMessage); return; } d.ExecuteReader(q, "", dataGridView1); } private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (c == 1 && r >= 0 && dataGridView1.Rows[r].Cells[c].Value.ToString() != "") { ReturnOrders_id = dataGridView1.Rows[r].Cells[c].Value.ToString(); this.Close(); } } } } |
Form9.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form9 : Form { public string DbHost; public string DbName; public string DbUser; public string DbPass; public Form9() { InitializeComponent(); try { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(string[])); using (System.IO.StreamReader sr = new System.IO.StreamReader(Application.StartupPath + @"\path.txt", new System.Text.UTF8Encoding(false))) { string[] s = (string[])xs.Deserialize(sr); textBox5.Text = s[0]; textBox1.Text = s[1]; textBox2.Text = s[2]; textBox3.Text = s[3]; sr.Close(); } } catch { } } private void Form9_FormClosing(object sender, FormClosingEventArgs e) { DbHost = textBox5.Text; DbName = textBox1.Text; DbUser = textBox2.Text; DbPass = textBox3.Text; try { System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(string[])); using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Application.StartupPath + @"\path.txt", false, new System.Text.UTF8Encoding(false))) { xs.Serialize(sw, new string[] { DbHost , DbName, DbUser , DbPass}); sw.Close(); } } catch { } } } } |
Form10.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 製品工数管理 { public partial class Form10 : Form { public string getDatetime = ""; public Form10() { InitializeComponent(); ImeMode = ImeMode.Off; for (int i = DateTime.Now.AddYears(-2).Year; i <= DateTime.Now.AddYears(2).Year; i++ ) { comboBox1.Items.Add(i.ToString()); } comboBox1.Text = DateTime.Now.Year.ToString(); for (int i = 1; i <= 12; i++) { comboBox2.Items.Add(i.ToString("00")); } comboBox2.Text = DateTime.Now.Month.ToString(); for (int i = 1; i <= 31; i++) { comboBox3.Items.Add(i.ToString("00")); } comboBox3.Text = DateTime.Now.Day.ToString(); for (int i = 0; i <= 24; i++) { comboBox4.Items.Add(i.ToString("00")); } comboBox4.Text = "00"; comboBox5.Items.AddRange(new string[] { "00", "15", "30", "45" }); comboBox5.Text = "00"; } private void button1_Click(object sender, EventArgs e) { DateTime dt = new DateTime(); try { dt = DateTime.Parse(comboBox1.Text + "-" + comboBox2.Text + "-" + comboBox3.Text + " " + comboBox4.Text + ":" + comboBox5.Text + ":01"); } catch { MessageBox.Show("値が正しくありません。"); getDatetime = ""; return; } getDatetime = dt.ToString("yyyy-MM-dd HH:mm:ss"); this.Close(); } private void button2_Click(object sender, EventArgs e) { getDatetime = ""; this.Close(); } } } |
Db.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace 製品工数管理 { class Db { public static string DbHost; public static string DbName; public static string DbUser; public static string DbPass; public Boolean DbState = true; public string DbStateMessage = ""; string dsn = ""; public Db() { dsn = @"user=" + DbUser + ";password=" + DbPass + ";database=" + DbName + ";server=" + DbHost + ";charset=sjis"; MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); } } catch(Exception e) { DbStateMessage = e.Message; DbState = false; return; } finally { con.Close(); } } public string[] LoadModel() { MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd = new MySqlCommand("select distinct model from orders", con); using (MySqlDataReader dr = cmd.ExecuteReader()) { List<string> tmp = new List<string>(); while (dr.Read()) { tmp.Add(dr[0].ToString()); } return tmp.ToArray(); } } } catch { } finally { con.Close(); } return new string[] { }; } public string[] LoadCustomer() { MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd = new MySqlCommand("select distinct customer from orders", con); using (MySqlDataReader dr = cmd.ExecuteReader()) { List<string> tmp = new List<string>(); while (dr.Read()) { tmp.Add(dr[0].ToString()); } return tmp.ToArray(); } } } catch { } finally { con.Close(); } return new string[] { }; } public string[][] LoadWorkers() { MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd = new MySqlCommand("select workers_id , worker_name from workers", con); using (MySqlDataReader dr = cmd.ExecuteReader()) { List<string[]> tmp = new List<string[]>(); while (dr.Read()) { tmp.Add(new string[] { dr[0].ToString(), dr[1].ToString() }); } return tmp.ToArray(); } } } catch{} finally { con.Close(); } return new string[][] { }; } public string ExecuteNonReader(string q, string cap) { MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd1 = new MySqlCommand(q,con); cmd1.ExecuteNonQuery(); MySqlCommand cmd2 = new MySqlCommand("select last_insert_id()",con); using (MySqlDataReader dr = cmd2.ExecuteReader()) { if (dr.Read() && dr[0].ToString() != "") return dr[0].ToString(); } } } catch { } finally { con.Close(); } return ""; } public void ExecuteReader(string q, string cap, DataGridView dgv) { dgv.Rows.Clear(); MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd = new MySqlCommand(q, con); using (MySqlDataReader dr = cmd.ExecuteReader()) { while(dr.Read()) { List<string> tmp = new List<string>(); for (int c = 0; c < dgv.Columns.Count; c++) { tmp.Add(dr[c].ToString()); } dgv.Rows.Add(tmp.ToArray()); } } } } catch { } finally { con.Close(); } } public Boolean QuantityCheck(string q, string quantity) { MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd = new MySqlCommand(q, con); using (MySqlDataReader dr = cmd.ExecuteReader()) { if(dr.Read() && dr[0].ToString() == quantity) return true; } } } catch { } finally { con.Close(); } return false; } public Boolean FormLoadExecuteReader(string q,List<TextBox> t) { MySqlConnection con = null; try { using (con = new MySqlConnection(dsn)) { con.Open(); MySqlCommand cmd = new MySqlCommand(q, con); using (MySqlDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { t[0].Text = dr[0].ToString(); t[1].Text = dr[1].ToString(); t[2].Text = dr[2].ToString(); t[3].Text = dr[3].ToString(); t[4].Text = dr[4].ToString(); return true; } } } } catch { } finally { con.Close(); } return false; } private static TimeSpan ActualWork(DateTime s, DateTime e) { //08:30 - 10:00 = 01:30 //10:15 - 12:00 = 01:45 = 03:15 //12:50 - 15:00 = 02:10 //15:15 - 17:20 = 02:05 = 04:15 = 07:30 int y = s.Year; int m = s.Month; int d = s.Day; DateTime start_dt = new DateTime(y, m, d, s.Hour, s.Minute, 00); DateTime end_dt = new DateTime(y, m, d, e.Hour, e.Minute, 00); DateTime dt = start_dt; TimeSpan ts = new TimeSpan(0, 0, 0); while (dt < end_dt) { ts += new TimeSpan(0, 1, 0); if (dt >= new DateTime(y, m, d, 10, 00, 00) && dt < new DateTime(y, m, d, 10, 15, 00)) ts -= new TimeSpan(0, 1, 0); if (dt >= new DateTime(y, m, d, 12, 00, 00) && dt < new DateTime(y, m, d, 12, 50, 00)) ts -= new TimeSpan(0, 1, 0); if (dt >= new DateTime(y, m, d, 15, 00, 00) && dt < new DateTime(y, m, d, 15, 15, 00)) ts -= new TimeSpan(0, 1, 0); if (dt >= new DateTime(y, m, d, 17, 20, 00) && dt < new DateTime(y, m, d, 17, 30, 00)) ts -= new TimeSpan(0, 1, 0); dt += new TimeSpan(0, 1, 0); } return ts; } public static string WorkTime(DateTime s, DateTime e, TimeSpan a) { if (s > e) return "-"; DateTime start_day = new DateTime(s.Year,s.Month,s.Day); DateTime end_day = new DateTime(e.Year, e.Month, e.Day); //開始と終了が同 if (start_day == end_day) { TimeSpan ts = ActualWork(s,e); return (ts + a).ToString().Substring(0, (ts + a).ToString().Length - 3); } else //開始と終了が別 { TimeSpan ts = new TimeSpan(0, 0, 0); int i = 0; while (start_day.AddDays(i) <= end_day) { DateTime t = start_day.AddDays(i); if (t == start_day && t != end_day)//開始 { ts += ActualWork(s, new DateTime(s.Year,s.Month,s.Day,17,20,00)); } if (t != start_day && t != end_day)//中間 { ts += ActualWork(new DateTime(t.Year, t.Month, t.Day, 8, 30, 00), new DateTime(t.Year, t.Month, t.Day, 17, 20, 00)); } if (t != start_day && t == end_day)//終了 { ts += ActualWork(new DateTime(e.Year, e.Month, e.Day, 8, 30, 00), e); } i++; } return (ts + a).ToString().Substring(0, (ts + a).ToString().Length - 3); } } } } |
WLI-TX4-G設定
WLI-TX4-Gをソフトウェアを使わず直接設定する方法。
初期化が必要な場合、ボタン長押しで初期化。
接続するPCのIPの設定は、ncpa.cpl→IPv4のプロパティ→詳細設定を開き、
1.1.1.2/255.255.255.0などを追加しておく。
あとは、http://1.1.1.1にアクセスすれば、root/空白でログインできる。
WinDdgを使ってMDMPファイルを見る
Win7を使っているので、
Microsoft Windows SDK for Windows 7 and .NET Framework 4
をインストールし、
Common Utilities → Debugging Tools for Windows
のみ選択。
しかし上手くいかず…。
Windows 10 Windows SDK(winsdksetup.exe)
を保存し、ショートカットを作成しオプションに/layoutをつける。
Debugging Tools for Windowsのみチェックをつけ実行。
C:\Users\xxx\Downloads\Windows Kits\10\WindowsSDK\Installers
の中にある
X86 Debuggers And Tools-x86_en-us.msi
を実行。
C# 預かり在庫
在庫管理の別バーション。方向性変更のため未使用。
出庫を別表にして視覚的に分かりやすく。
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
using System; using System.Windows.Forms; using System.Data.OleDb; namespace 預かり在庫管理_1._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.CellClick += new DataGridViewCellEventHandler(cell_click); using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("select * from order_tbl", con); cmd.ExecuteNonQuery(); } catch { MessageBox.Show("データベースに接続できませんでした。"); return; } finally { con.Close(); } } load_Db("select * from order_tbl where 表示 = 0"); } void cell_click(object sener, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; string id = dataGridView1.Rows[r].Cells[0].Value.ToString(); string number = dataGridView1.Rows[r].Cells[1].Value.ToString(); string client = dataGridView1.Rows[r].Cells[2].Value.ToString(); string item = dataGridView1.Rows[r].Cells[3].Value.ToString(); string quantity = dataGridView1.Rows[r].Cells[4].Value.ToString(); string v = dataGridView1.Rows[r].Cells[7].Value.ToString(); if (r == -1 || c == -1) return; if (c == 0)//ID { Form f = new Form3(id, number, client, item, quantity); f.StartPosition = FormStartPosition.CenterParent; f.ShowDialog(); this.button3.PerformClick(); } if (c == 5)//編集 { Form f = new Form2(id, number, client, item, quantity, v); f.StartPosition = FormStartPosition.CenterParent; f.ShowDialog(); this.button3.PerformClick(); } if (c == 6)//削除 { DialogResult yn = MessageBox.Show("削除します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("delete from order_tbl where ID = " + id + "", con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } } this.button3.PerformClick(); } } void load_Db(string q) { dataGridView1.Rows.Clear(); using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(q, con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { this.dataGridView1.Rows.Add( dr["ID"].ToString(), dr["伝票番号"].ToString(), dr["仕入先"].ToString(), dr["品目名"].ToString(), dr["数量"].ToString(), "編集", "削除", dr["表示"].ToString() ); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } } //初期化 private void button3_Click(object sender, EventArgs e) { load_Db("select * from order_tbl where 表示 = 0"); textBox1.Enabled = true; textBox1.Text = ""; comboBox1.Text = ""; } //検索 private void button2_Click(object sender, EventArgs e) { if(comboBox1.Text == "非表示品") { load_Db("select * from order_tbl where 表示 = 1"); return; } if (textBox1.Text == "" || comboBox1.Text == "") { MessageBox.Show("検索条件が不足です。"); return; } load_Db("select * from order_tbl where 表示 = 0 and " + comboBox1.Text + " like '%" + textBox1.Text + "%'"); } private void button1_Click(object sender, EventArgs e) { Form f = new Form2("","","","","",""); f.StartPosition = FormStartPosition.CenterParent; f.ShowDialog(); this.button3.PerformClick(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.Text == "非表示品") { textBox1.Text = ""; textBox1.Enabled = false; } else { textBox1.Enabled = true; } } } } |
Form2.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
using System; using System.Windows.Forms; using System.Data.OleDb; namespace 預かり在庫管理_1._0 { public partial class Form2 : Form { public Form2(string id, string number, string client, string item, string quantity, string v) { InitializeComponent(); textBox5.Text = id; textBox1.Text = number; comboBox1.Text = client; textBox3.Text = item; textBox4.Text = quantity; if (textBox5.Text == "") { checkBox1.Enabled = false; } else { checkBox1.Enabled = true; } if (v == "1") checkBox1.Checked = true; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("select distinct 仕入先 from order_tbl", con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { comboBox1.Items.Add(dr["仕入先"].ToString()); } } } finally { con.Close(); } } } private void button1_Click(object sender, EventArgs e) { textBox5.Text = textBox5.Text.Replace("\"", "”"); textBox1.Text = textBox1.Text.Replace("\"", "”"); comboBox1.Text = comboBox1.Text.Replace("\"", "”"); textBox3.Text = textBox3.Text.Replace("\"", "”"); textBox4.Text = textBox4.Text.Replace("\"", "”"); textBox5.Text = textBox5.Text.Replace("\'", "’"); textBox1.Text = textBox1.Text.Replace("\'", "’"); comboBox1.Text = comboBox1.Text.Replace("\'", "’"); textBox3.Text = textBox3.Text.Replace("\'", "’"); textBox4.Text = textBox4.Text.Replace("\'", "’"); try { if (textBox1.Text == "" || comboBox1.Text == "" || textBox3.Text == "") throw new Exception(); short.Parse(textBox4.Text); } catch { MessageBox.Show("入力に不正があります。"); return; } string q = ""; if (textBox5.Text == "") { q = "insert into order_tbl(伝票番号,仕入先,品目名,数量,表示) values('" + textBox1.Text + "','" + comboBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "',0)"; } else { string v = "0"; if (checkBox1.Checked) v = "1"; q = "update order_tbl set 伝票番号='" + textBox1.Text + "',仕入先='" + comboBox1.Text + "',品目名='" + textBox3.Text + "',数量=" + textBox4.Text + ",表示=" + v + " where ID = " + textBox5.Text + ""; } using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(q, con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } } this.Close(); } } } |
Form3.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
using System; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using System.IO; namespace 預かり在庫管理_1._0 { public partial class Form3 : Form { public Form3(string id, string number, string client, string item, string quantity) { InitializeComponent(); textBox5.Text = id; textBox1.Text = number; textBox2.Text = client; textBox3.Text = item; textBox4.Text = quantity; dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView2.AllowUserToAddRows = false; dataGridView2.ReadOnly = true; dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView2.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView2.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.CellClick += new DataGridViewCellEventHandler(cell_click_dvg1); dataGridView2.CellClick += new DataGridViewCellEventHandler(cell_click_dvg2); load_db_dgv(); } void open_Folder(string number) { string path = ""; try { using (StreamReader sr = new StreamReader(Application.StartupPath + @"\setting.txt", Encoding.GetEncoding("shift_jis"))) { path = sr.ReadLine() + @"\" + textBox2.Text + @"\" + number; if (Directory.Exists(path)) { System.Diagnostics.Process.Start(path); } else { DialogResult yn = MessageBox.Show("フォルダを作成します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; Directory.CreateDirectory(path); System.Diagnostics.Process.Start(path); } } } catch(Exception e) { MessageBox.Show(e.Message); return; } } void cell_click_dvg2(object sener, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (r == -1 || c == -1) return; if (c == 7) open_Folder(dataGridView2.Rows[r].Cells[7].Value.ToString());//管理番号 if (c == 9)//戻入 { DialogResult yn = MessageBox.Show("在庫に戻します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("update stock_tbl set 出庫日='',出荷=0,PO='',管理番号='',得意先='' where ID = " + dataGridView2.Rows[r].Cells[0].Value.ToString() + "", con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } } load_db_dgv(); } } void cell_click_dvg1(object sener, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (r == -1 || c == -1) return; if(c == 3)//分割 { Form f = new Form4(dataGridView1.Rows[r].Cells[0].Value.ToString(),dataGridView1.Rows[r].Cells[3].Value.ToString()); f.StartPosition = FormStartPosition.CenterParent; f.ShowDialog(); load_db_dgv(); } if (c == 5)//出荷 { if (textBox8.Text == "" || comboBox2.Text == "" || textBox12.Text == "") { MessageBox.Show("出庫日・得意先・管理番号(INV)は必須です。"); return; } comboBox2.Text = comboBox2.Text.Replace("\"", "”"); comboBox2.Text = comboBox2.Text.Replace("\'", "’"); textBox8.Text = textBox8.Text.Replace("\"", "”"); textBox8.Text = textBox8.Text.Replace("\'", "’"); textBox11.Text = textBox11.Text.Replace("\"", "”"); textBox11.Text = textBox11.Text.Replace("\'", "’"); DialogResult yn = MessageBox.Show("出荷します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("update stock_tbl set 出庫日 = '" + textBox12.Text + "',出荷 = 1,PO = '" + textBox11.Text + "',管理番号='" + textBox8.Text + "',得意先='" + comboBox2.Text + "' where ID = " + dataGridView1.Rows[r].Cells[0].Value.ToString() + "", con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } } load_db_dgv(); } if (c == 6)//削除 { DialogResult yn = MessageBox.Show("削除します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand("delete from stock_tbl where ID = " + dataGridView1.Rows[r].Cells[0].Value.ToString() + "", con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } } load_db_dgv(); } } void load_db_dgv() { dataGridView1.Rows.Clear(); dataGridView2.Rows.Clear(); comboBox2.Items.Clear(); string id = textBox5.Text; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd1 = new OleDbCommand("select * from stock_tbl where 受注ID = " + id + " and 出荷 = 0", con); using (OleDbDataReader dr1 = cmd1.ExecuteReader()) { while (dr1.Read()) { this.dataGridView1.Rows.Add( dr1["ID"].ToString(), dr1["受注ID"].ToString(), dr1["日付"].ToString(), dr1["数量"].ToString(), dr1["種類"].ToString(),"出荷", "削除"); } } OleDbCommand cmd2 = new OleDbCommand("select * from stock_tbl where 受注ID = " + id + " and 出荷 = 1", con); using (OleDbDataReader dr2 = cmd2.ExecuteReader()) { while (dr2.Read()) { this.dataGridView2.Rows.Add( dr2["ID"].ToString(), dr2["受注ID"].ToString(), dr2["日付"].ToString(), dr2["出庫日"].ToString(), dr2["数量"].ToString(), dr2["種類"].ToString(), dr2["得意先"].ToString(), dr2["管理番号"].ToString(), dr2["PO"].ToString(), "戻入"); } } OleDbCommand cmd3 = new OleDbCommand("select distinct 得意先 from stock_tbl where 出荷 = 1", con); using (OleDbDataReader dr3 = cmd3.ExecuteReader()) { while (dr3.Read()) { comboBox2.Items.Add(dr3["得意先"].ToString()); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } int ship_qty = 0; int stock_qty = 0; for (int r = 0; r < dataGridView1.Rows.Count; r++) { stock_qty = stock_qty + int.Parse(dataGridView1.Rows[r].Cells[3].Value.ToString()); } for (int r = 0; r < dataGridView2.Rows.Count; r++) { ship_qty = ship_qty + int.Parse(dataGridView2.Rows[r].Cells[4].Value.ToString()); } textBox9.Text = stock_qty.ToString();//在庫数量 textBox10.Text = ship_qty.ToString();//出庫数量 label14.Text = "(" + (stock_qty + ship_qty).ToString() + ")"; textBox6.Text = DateTime.Now.ToString("yyyy/MM/dd");//入庫日 textBox12.Text = DateTime.Now.ToString("yyyy/MM/dd");//出庫日 textBox7.Text = "";//数量 textBox8.Text = "";//管理番号(INV) textBox11.Text = "";//PO comboBox2.Text = "";//得意先 comboBox1.Text = "";//保管方法 } //登録 private void button1_Click(object sender, EventArgs e) { string id = textBox5.Text; try { if (comboBox1.Text == "" || textBox6.Text == "") throw new Exception(); short.Parse(textBox7.Text); } catch { MessageBox.Show("入力に不正があります。"); return; } string q = "insert into stock_tbl (受注ID,日付,数量,種類,出荷) values (" + id + ",'" + textBox6.Text + "'," + textBox7.Text + ",'" + comboBox1.Text + "',0)"; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(q, con); cmd.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } load_db_dgv(); } } private void button2_Click(object sender, EventArgs e) { Form f = new Form5(); f.StartPosition = FormStartPosition.CenterParent; f.ShowDialog(); } } } |
Form4.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; using System.Windows.Forms; using System.Data.OleDb; namespace 預かり在庫管理_1._0 { public partial class Form4 : Form { short quantity = 0; int id = 0; public Form4(string i, string q) { InitializeComponent(); quantity = short.Parse(q); id = int.Parse(i); textBox6.Text = "0"; } private void button1_Click(object sender, EventArgs e) { short number = 0; try { if (textBox6.Text == "") throw new Exception(); number = short.Parse(textBox6.Text); if (quantity <= number || number == 0) throw new Exception(); } catch { MessageBox.Show("入力に不正があります。"); return; } DialogResult yn = MessageBox.Show("分割します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); string q = "insert into stock_tbl (受注ID,日付,数量,種類,出荷) select 受注ID,日付,'" + number.ToString() + "',種類,'0' from stock_tbl where ID = " + id.ToString() + ""; OleDbCommand cmd1 = new OleDbCommand(q, con); cmd1.ExecuteNonQuery(); q = "update stock_tbl set 数量 = " + (quantity - number).ToString() +" where ID = " + id.ToString() + ""; OleDbCommand cmd2 = new OleDbCommand(q, con); cmd2.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } finally { con.Close(); } } this.Close(); } } } |
Form5.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
using System; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using System.IO; namespace 預かり在庫管理_1._0 { public partial class Form5 : Form { public Form5() { InitializeComponent(); dataGridView1.AllowUserToAddRows = false; dataGridView1.ReadOnly = true; dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; dataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; dataGridView1.CellClick += new DataGridViewCellEventHandler(cell_click); string q = "select 伝票番号,仕入先,品目名,日付,stock_tbl.数量,種類,管理番号,得意先,PO,出庫日 " + "from stock_tbl left outer join order_tbl " + "on stock_tbl.受注ID = order_tbl.ID where 出荷 = 1"; load_Db(q); } void load_Db(string q) { dataGridView1.Rows.Clear(); using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand cmd = new OleDbCommand(q, con); using (OleDbDataReader dr = cmd.ExecuteReader()) { while (dr.Read()) { this.dataGridView1.Rows.Add( dr["伝票番号"].ToString(), dr["仕入先"].ToString(), dr["品目名"].ToString(), dr["日付"].ToString(), dr["出庫日"].ToString(), dr["数量"].ToString(), dr["種類"].ToString(), dr["得意先"].ToString(), dr["管理番号"].ToString(), dr["PO"].ToString() ); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } } //初期化 private void button3_Click(object sender, EventArgs e) { string q = "select 伝票番号,仕入先,品目名,日付,stock_tbl.数量,種類,管理番号,得意先,PO,出庫日 " + "from stock_tbl left outer join order_tbl " + "on stock_tbl.受注ID = order_tbl.ID where 出荷 = 1"; load_Db(q); textBox1.Text = ""; comboBox1.Text = ""; } //検索 private void button2_Click(object sender, EventArgs e) { if (textBox1.Text == "" || comboBox1.Text == "") { MessageBox.Show("検索条件が不足です。"); return; } string param = ""; switch (comboBox1.Text) { case "入庫日": param = "日付"; break; case "管理番号(INV)": param = "管理番号"; break; default: param = comboBox1.Text; break; } string q = "select 伝票番号,仕入先,品目名,日付,stock_tbl.数量,種類,管理番号,得意先,PO,出庫日 " + "from stock_tbl left outer join order_tbl " + "on stock_tbl.受注ID = order_tbl.ID where 出荷 = 1 and " + "" + param + " like '%" + textBox1.Text + "%'"; load_Db(q); } void cell_click(object sener, DataGridViewCellEventArgs e) { int r = e.RowIndex; int c = e.ColumnIndex; if (r == -1 || c == -1) return; if (c == 8) { string number = dataGridView1.Rows[r].Cells[8].Value.ToString(); string client = dataGridView1.Rows[r].Cells[1].Value.ToString(); try { using (StreamReader sr = new StreamReader(Application.StartupPath + @"\setting.txt", Encoding.GetEncoding("shift_jis"))) { string path = sr.ReadLine() + @"\" + client + @"\" + number; if (Directory.Exists(path)) { System.Diagnostics.Process.Start(path); } else { DialogResult yn = MessageBox.Show("フォルダを作成します。実行しますか?", "", MessageBoxButtons.YesNo); if (yn == DialogResult.No) return; Directory.CreateDirectory(path); System.Diagnostics.Process.Start(path); } } } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } } } } |
SQL インデックス
基本はB-tree(B+tree)インデックスが利用されることが多い。
特徴としては、ノードにキーを持ち、リーフノードにポインタがある。作成時はキーをソートする。
リーフノードまでの距離が均一でバランスがいいのが特徴。
インデックスが必要な箇所
大規模(数万以上)なテーブル。規模が小さいとフルスキャンと大差ない。
カーディナリティ(入る値の種類数のこと)が高い列。さらに入る値が集中していないこと。
検索条件や結合条件の列以外は無意味。(検索条件内でもインデックスの列で演算などするとダメ)注意点として、否定形(<>)やORやLIKE演算子はダメで、暗黙の型変換もダメ。
主キーは既にインデックスされているので不要。
Windows Defenderの無効化
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender]
“DisableAntiSpyware”=dword:00000001
セキュリティソフト入れている場合、スタートアップから、
Windows Defender notification iconを消すだけで大丈夫なこともある。