2016-08-25 51 views
0

我確實試圖找到示例,語法等,但它總是將我重定向到excel問題。也許我使用了完全錯誤的方法。外部應用程序寫入訪問數據庫

因此,我正在用vba代碼編寫visual studio 2015的Windows應用程序。 該程序主要收集輸入到文本框中的信息,然後保存在小型數據庫中。 最初我使用Excel作爲我的「測試數據庫」,但主要問題是它只能被一個用戶訪問,它顯然不適合數據庫。現在我想更改我的代碼來使用MS Access數據庫,但是我無法圍繞如何做到這一點。 我絕對不熟悉DB使用編碼:(

的代碼爲我的舊程序的Excel:

' Start Excel and get Application object. 
    oXL = New Excel.Application 

    ' Set some properties 
    oXL.Visible = False 
    oXL.DisplayAlerts = False  

    Dim wkbk As Excel.Workbook = oXL.Workbooks.Add 

    'add(worksheet) 

    wkbk.Worksheets.Add() 

    'open existing work   

    oWB = oXL.Workbooks.Open("C:\database\Productionplan.xlsx", ReadOnly:=False) 

    ' Get the active sheet 
    oSheet = DirectCast(oWB.ActiveSheet, Excel.Worksheet) 
    oSheet.Name = "Sheet1" 

    oSheet.Cells(index, 3).value = po.Text 
    oSheet.Cells(index, 4).Value = product.Text 
    oSheet.Cells(index, 5).Value = drawings.Text 
    oSheet.Cells(index, 6).Value = customer.Text 
    oSheet.Cells(index, 7).Value = assemblingdate.Text 
    oSheet.Cells(index, 8).Value = deliverydate.Text 
    oSheet.Cells(index, 9).Value = voltage.Text 
    oSheet.Cells(index, 10).Value = power.Text 
    oSheet.Cells(index, 11).Value = serialno.Text 
    oSheet.Cells(index, 12).Value = quantity.Text 
    oSheet.Cells(index, 13).Value = unit.Text 


    ' Save the sheet and close 
    oSheet = Nothing 
    oRange = Nothing 
    oWB.SaveAs("C:\database\Productionplan.xlsx") 
    oWB.Close() 
    oWB = Nothing 
    oXL.Quit() 

是否有類似的簡單的方法來寫Access數據庫中值插入特定的細胞 表示感謝?如果有人知道教程網站或其他什麼東西,那將會很有幫助,只要我有一些簡單的示例代碼來訪問數據庫中的單元格,我應該能夠計算出如何將代碼傳輸到數據庫。

最好的問候 Dom

編輯:從文本框添加一些圖片 datagrid

application

+0

數據庫沒有「單元格」。他們有表格,有記錄,有字段。搜索ADODB和DAO。 – Comintern

+0

我確實使用OLEDB來連接並讀取我的表格,我也能夠將新記錄添加到表格中。但我不知道如何通過使用我的程序變量來計算特定的記錄等。我假設我必須使用一些查詢來做到這一點? – Dom87

+0

您可以使用非標量sql語句進行插入(INSERT INTO)和編輯(UPDATE [table] SET ...)您可以使用標量sql語句(SELECT COUNT(*)FROM ...)進行計數。 – Comintern

回答

0

事情是這樣的,其中字段1,...將是表的列名,而 「值1」,...

Dim objRS As Object ' optional 
Set objRS = CreateObject("ADODB.Recordset") 

objRS.Open "INSERT INTO [table 1] ([field 1] , [field 2]) " & _ 
         " VALUES (""value 1"",""value 2"");", _ 
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\test.mdb" 

Set objRS = Nothing ' optional 
相關問題