前提・実現したいこと
ACCESSで作成したデータをDataGridViewに表示させたいです。
エラーはでていません。
該当のソースコード
下記は全体のソースコードです。
Imports Microsoft.Office.Interop Imports System.Data.OleDb Public Class Form1 Dim str, syukkinJikan, syukkinHun, taikinJikan, taikinHun As Integer 'パスワード Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged End Sub 'パスワード入力規制 Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating If System.Text.RegularExpressions.Regex.IsMatch(TextBox1.Text, "[^0-9]+") Then MsgBox("数字以外の文字は入力できません。") e.Cancel = True End If End Sub '日付 Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged End Sub '出勤時間(時) Private Sub ComboBox1_SelectedIndexChanged_1(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged End Sub '出勤時間(分) Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged End Sub '退勤時間(時) Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged End Sub '退勤時間(分) Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click '変数宣言 Dim ex As New Microsoft.Office.Interop.Excel.Application Dim sh As Microsoft.Office.Interop.Excel.Worksheet Dim wb As Microsoft.Office.Interop.Excel.Workbook 'ファイルオープン wb = ex.Workbooks.Open("C:\Users\k_sat\Desktop\給料計算.xlsx") sh = wb.Sheets("Sheet1") '給料をエクセルに表示させる sh.Range("F3").Value = TextBox2.Text wb.Save() ex.Quit() End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'SQL作成 Dim resultDt As New DataTable Dim sql = New System.Text.StringBuilder() sql.AppendLine("SELECT") sql.AppendLine(" * ") sql.AppendLine("FROM ID給料") 'Access接続準備 Dim command As New OleDbCommand Dim da As New OleDbDataAdapter Dim cnAccess As OleDbConnection = New OleDbConnection cnAccess.ConnectionString = My.Settings.アクセス 'Access接続開始 cnAccess.Open() Try command.Connection = cnAccess command.CommandText = sql.ToString da.SelectCommand = command 'SQL実行 結果をデータテーブルに格納 da.Fill(resultDt) Catch ex As Exception Throw Finally command.Dispose() da.Dispose() cnAccess.Close() End Try 'データテーブルの結果を表示 For rowindex As Integer = 0 To resultDt.Rows.Count - 1 For colindex As Integer = 0 To resultDt.Columns.Count - 1 Console.Write(resultDt.Rows(rowindex).Item(colindex).ToString & " ") Next Console.WriteLine() Next End Sub Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick End Sub '確認ボタン(コンボボックスのデータを取得) Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click syukkinJikan = ComboBox1.SelectedItem syukkinHun = ComboBox2.SelectedItem taikinJikan = ComboBox3.SelectedItem taikinHun = ComboBox4.SelectedItem '稼働時間(残業、深夜)計算 Dim kadouJikan, kadouHun As Integer If taikinJikan > syukkinJikan Then kadouJikan = taikinJikan - syukkinJikan ElseIf taikinJikan < syukkinJikan Then kadouJikan = taikinJikan - syukkinJikan + 24 End If If taikinHun > syukkinHun Then kadouHun = taikinHun - syukkinHun ElseIf taikinHun < syukkinHun Then kadouHun = taikinHun - syukkinHun + 60 End If Dim kyuukeiJikan, kyuukeiHun As Integer If kadouJikan < 6 Then kyuukeiHun = kadouHun kyuukeiJikan = kadouJikan ElseIf kadouJikan >= 6 And kadouJikan < 8 And kadouHun > 45 Then kyuukeiHun = kadouHun - 45 kyuukeiJikan = kadouJikan - 1 ElseIf kadouJikan >= 6 And kadouJikan < 8 And kadouHun < 45 Then kyuukeiHun = kadouHun - 45 + 60 kyuukeiJikan = kadouJikan - 1 ElseIf kadouJikan >= 8 Then kyuukeiHun = kadouHun kyuukeiJikan = kadouJikan - 1 End If Dim ZangyouJikan As Integer If kyuukeiJikan < 8 Then ElseIf kyuukeiJikan >= 8 Then ZangyouJikan = kyuukeiJikan - 8 End If Dim kyuuyo, kyuuyoZan, kyuuyoKakutei As Integer If kyuukeiJikan < 8 Then kyuuyoKakutei = (kyuukeiJikan * 1500 + (kyuukeiHun / 60) * 1500) ElseIf kyuukeiJikan >= 8 Then kyuuyo = (8 * 1500 + (kyuukeiHun / 60) * 1500) kyuuyoZan = ((kyuukeiJikan - 8) * 1500 * 1.25 + ((kyuukeiHun / 60) * 1500 * 1.25)) kyuuyoKakutei = kyuuyo + kyuuyoZan End If '一日分の給料を表示 TextBox2.Text = kyuuyoKakutei '変数宣言 Dim ex As New Microsoft.Office.Interop.Excel.Application Dim sh As Microsoft.Office.Interop.Excel.Worksheet Dim wb As Microsoft.Office.Interop.Excel.Workbook 'ファイルオープン wb = ex.Workbooks.Open("C:\Users\k_sat\Desktop\給料計算.xlsx") sh = wb.Sheets("Sheet1") '出勤時間、退勤時間をエクセルに表示させる sh.Range("A3").Value = DateTimePicker1.Text sh.Range("B3").Value = ComboBox1.SelectedItem sh.Range("C3").Value = ComboBox2.SelectedItem sh.Range("D3").Value = ComboBox3.SelectedItem sh.Range("E3").Value = ComboBox4.SelectedItem wb.Save() ex.Quit() End Sub '給料 Private Sub Label10_Click(sender As Object, e As EventArgs) Handles Label10.Click End Sub '給料 Public Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged End Sub End Class
下記は問題と思われる部分(68行目~112行目)のソースコードです。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'SQL作成 Dim resultDt As New DataTable Dim sql = New System.Text.StringBuilder() sql.AppendLine("SELECT") sql.AppendLine(" * ") sql.AppendLine("FROM ID給料") 'Access接続準備 Dim command As New OleDbCommand Dim da As New OleDbDataAdapter Dim cnAccess As OleDbConnection = New OleDbConnection cnAccess.ConnectionString = My.Settings.アクセス 'Access接続開始 cnAccess.Open() Try command.Connection = cnAccess command.CommandText = sql.ToString da.SelectCommand = command 'SQL実行 結果をデータテーブルに格納 da.Fill(resultDt) Catch ex As Exception Throw Finally command.Dispose() da.Dispose() cnAccess.Close() End Try 'データテーブルの結果を表示 For rowindex As Integer = 0 To resultDt.Rows.Count - 1 For colindex As Integer = 0 To resultDt.Columns.Count - 1 Console.Write(resultDt.Rows(rowindex).Item(colindex).ToString & " ") Next Console.WriteLine() Next End Sub
自分で調べたことや試したこと
参考にしたサイト
https://www.fenet.jp/dotnet/column/%e8%a8%80%e8%aa%9e%e3%83%bb%e7%92%b0%e5%a2%83/928/
ACCESSのデータをdatagridviewに表示させるためにボタンを押下すると、
画面左下に「System.EnterpriseServices.Wrapper.dllのシンボルを読み込んでます」
と表示されます。
使っているツールのバージョンなど補足情報
ACCESSのバージョン2016
Windows10の64ビット
WindowsForms
Visual Studio2019
NET Framework 4.8
AccessDatabaseEngine.exeインストール済です。
上記のコードではDataGridViewに表示されず詰んでおります。
ご教示よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー