检查2个字段后使用ado vb.net保存数据访问2003 db [英] Saving data using ado vb.net after checking 2 fields access 2003 db

查看:77
本文介绍了检查2个字段后使用ado vb.net保存数据访问2003 db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

绅士,

在检查两个条件后,我想保存产品表.

在产品表中,我具有字段STOCKCODE(数据类型为TEXT)(允许重复输入),而BranchName =数据类型为Text.

可以说我已经在BRANCHNAME NEW BRANCH中保存了StockCode Ms Office软件.现在,我要保存另一个具有相同股票代码的StockCode,即Office女士软件,但保存在同一Products表中另一个名为OLD BRANCH的分支中.

我正在使用以下代码,但给出错误

MainModule.vb下的代码

Gents,

I want to save the products table after checking two conditions.

In products table I have fields STOCKCODE (datatype TEXT) (Allows duplicate entries) and BranchName = Data type Text.

Lets say I have already saved a StockCode Ms Office software in BRANCHNAME NEW BRANCH. Now, I want to save another StockCode with same stock code i.e Ms Office software but in another branch called OLD BRANCH in the same Products table.

I am using the following code but its giving error

Code under the MainModule.vb

Public Function IsStockExists(ByVal stockname As String, ByVal BranchName As String) As Boolean


       stockname = Replace(stockname, " ", "")
       Dim Dbf As New ADODB.Recordset
       Dbf.Open("Select * from stockdbf where tempstockcode='" + stockname + "' and BranchName=" & BranchName, Conn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)



--------->> Error    (Syntax error (missing operator) in query expression 'tempstockcode='La457' and Branchname='.)

       If Dbf.RecordCount > 0 Then
           Dbf.Close()
           Return True
       Else
           Dbf.Close()
           Return False
       End If
   End Function



AddButton上ProductsForm下的代码




Code under ProductsForm on AddButton


 Private Sub UserButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
If IsStockExists(txtstockcode.Text, cmbBranches.Text) = True Then
            MsgBox("The Stock Item Already exists, Please Try Again")
            Exit Sub
        End If

        If MsgBox(" Do u want to save ?                    ", MsgBoxStyle.YesNo + MsgBoxStyle.Information) = MsgBoxResult.Yes Then
            SaveStock()
            txtstockcode.Focus()
        End If
    End Sub



任何人都可以在上述问题上为我提供帮助.在此先感谢..



Can anyone help me out on the above issue. Thanks in advance..

推荐答案

当前,您正在检查是否存在行,而不是实际数据...

用我的功能替换您的功能,并在必要时进行编辑...这可以减轻您的麻烦.


Currently you are checking if a row exists and not the actual data...

Replace your function with mine and edit where necessary... This should ease your troubles.


Public Function IsStockExists(ByVal stockname As String, ByVal BranchName As String) As Boolean

    Dim stockExists As Boolean
    Dim DatabaseLocation As String = "" ' ADD YOUR DATABASE PATH HERE!!!
    ' Assuming if you have a password on your database the connection string will look like this...
    ' "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseLocation & ";Jet Oledb:Database Password=yourpassword"
    Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseLocation
    Dim accessConnection As New OleDbConnection(ConnectionString)
    Dim query As String = "Select * from [DATABASE TABLE] where [tempstockcode]='" + stockname + "' and [BranchName]='" + BranchName + "'"
    Dim objcmd As New OleDbCommand(query, accessConnection)

    Try
        If accessConnection.State = ConnectionState.Closed Then
            accessConnection.Open()
        End If
        Dim objReader As OleDbDataReader = objcmd.ExecuteReader
        If objReader.Read = True Then
            stockExists = True
        Else
            stockExists = False
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message & ex.StackTrace)
    End Try
        If accessConnection.State = ConnectionState.Open Then
            accessConnection.Close()
        End If
    Return stockExists
End Function


这篇关于检查2个字段后使用ado vb.net保存数据访问2003 db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆