Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer peopleโ€™s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer peopleโ€™s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:34:00+00:00 2026-05-16T15:34:00+00:00

i should say hi experts :D . Help me with this pretty code :)

  • 0

i should say hi experts ๐Ÿ˜€ . Help me with this pretty code ๐Ÿ™‚

The database:

“ID (Primary key)” | “Title”
0 | “title1”
1 | “title2”
2 | “title3”
3 | “title4”


Sub AddRecord(ByVal Table As String, ByVal Columns As String, ByVal Record() As String)
    Dim SubDir As String = ""

    Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM " & Table, connectionString)
    Dim command As OleDbCommand
    Dim tbCorrectSyntax = ""

    Using connection As New OleDbConnection(connectionString)
        Try
            connection.Open()
            Dim Commandtxt As String = ""
            For i = 0 To Record.GetUpperBound(0)
                If Record(i).IndexOf(",") > 0 Then
                    Dim tmpRec() As String = Nothing
                    Dim cols() As String = Nothing

                    tmpRec = Record(i).Split(",")
                    cols = Columns.Split(",")

                    For j = 0 To tmpRec.GetUpperBound(0)
                        tbCorrectSyntax &= cols(j) & " = '" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " , ")
                    Next
                End If


                Dim txtCorrect As String = IIf(tbCorrectSyntax = "", Columns & " = " & Record(i), tbCorrectSyntax)

                Commandtxt = "IF OBJECT_ID ( 'InsertOrUpdateItem', 'P' ) IS NOT NULL " & _
                                "DROP PROCEDURE InsertOrUpdateItem " & _
                             "GO " & _
                             "CREATE PROCEDURE InsertOrUpdateItem " & _
                                "AS " & _
                                "IF (EXISTS (SELECT * FROM " & Table & _
                                            " WHERE " & txtCorrect & "))" & _
                                            " begin " & _
                                                "UPDATE (" & Table & ") " & _
                                                txtCorrect & _
                                                " WHERE " & txtCorrect & " " & _
                                            " End " & _
                                            " else " & _
                                                " begin " & _
                                                " INSERT INTO " & Table & " (" & Columns & ") " & _
                                                " VALUES (" & Record(i) & ")" & _
                                            " End " & _
                                "End "

                'Commandtxt = "INSERT INTO " & Table & " (" & Columns & ") VALUES (" & Record(i) & ")"
                command = New OleDbCommand(Commandtxt, connection)
                command.CommandType = CommandType.StoredProcedure
                command.ExecuteNonQuery()

            Next


        Catch ex As Exception
            msgbox(ex.Message)
        Finally
            connection.Close()
        End Try
    End Using
End Sub

Function connectionString() As String
    Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBdir & ";User Id=admin;Password=;"
End Function

The first procedure is a wrapper to add data to database fields (if the data doent exist)
But update it the row with new data already exist.

Input:
Table = TableName
Columns = name of colomns that will be updated separated by comma (Ex1: “ID” , Ex2: “ID,Title,…”)
Record() = string array that represent the new values( multiple values are separated with comma)

OK, before adding values to database, we should check if a row exists with this values ๐Ÿ™‚
TO do this, creating a Stored Procedure is a best way to deal with the database fastly.

So… The problem now is, at the runtime, Miss OleDB throw this error:
Microsoft Jet database engine cannot find the input table or query ‘IF’ ….

Thanks in advance ๐Ÿ˜€

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:34:01+00:00Added an answer on May 16, 2026 at 3:34 pm

    I found a solution for my problem (with a little net research ๐Ÿ™‚ ) hahahaha i m happy
    anyway, the initial question was: ‘How to update a record in database if it exists’
    So i tryed to create and store a Stored procedure in the database… but… ๐Ÿ™‚

    Then i found an interesting Method: the ExecuteScalar of OleDBcommand Class

    It simply return a value according to Sql input : in the following example that i used, it return the index (primary key) if the recod exists. So lets begin:

    Function GetRecordIndex(ByVal Table As String, ByVal Columns As String, ByVal Record As String) As String
        Dim Commandtxt As String = ""
        Dim Command As OleDbCommand
        Dim tbCorrectSyntax As String = ""
        Dim tbCorrectSyntaxAND As String = ""
    
        Using connection As New OleDbConnection(connectionString)
    
            Try
                connection.Open()
                If Record.IndexOf(",") > 0 Then
                    Dim tmpRec() As String = Nothing
                    Dim cols() As String = Nothing
    
                    tmpRec = Record.Split(",")
                    cols = Columns.Split(",")
    
                    For j = 0 To tmpRec.GetUpperBound(0)
                        tbCorrectSyntax &= cols(j) & "='" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " , ")
                        tbCorrectSyntaxAND &= cols(j) & "='" & tmpRec(j) & "' " & IIf(j = tmpRec.GetUpperBound(0), "", " AND ")
                    Next
                End If
                Dim txtCorrect As String = IIf(tbCorrectSyntax = "", Columns & "=" & Record, tbCorrectSyntax)
                Dim txtCorrectAND As String = IIf(tbCorrectSyntaxAND = "", Columns & "=" & Record, tbCorrectSyntaxAND)
    
                Commandtxt = "SELECT * FROM " & Table & " WHERE " & txtCorrectAND
                Command = New OleDbCommand(Commandtxt, connection)
                Dim RecordIndex As String = Command.ExecuteScalar
    
                Return RecordIndex
    
            Catch ex As Exception
                Return Nothing
            Finally
                connection.Close()
            End Try
        End Using
    End Function
    

    As before, Columns Parameter can be a single Database column, or multiple columns separated with comma . same thing with Record that represent the data inside each column

    Thank for your help
    Fadelovesky

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As i am in between 1-2 years experience what should i say to this
A question to windows network programming experts. When I use pseudo-code like this: reconnect:
Question should say it all. Let's say there's a local file mydefaultvalues.txt, separated from
I should say I'm looking for something interactive, equivalent to what Nevron offers in
First off I should say that I don't have any experience in working with
First, i should say that I am relatively new to programming so please be
I tried the following: Basically, it should say: If there's no cookie, get lang
I want to find how much folders are in folder or I should say
I wan't to divide my CI into multiple applications. I should say that it
Say,should keep the space between letters(a-z,case insensitive) and remove the space between non-letters?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.