Results 1 to 7 of 7

Thread: Database Contains

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Database Contains

    This is what ive made so far:
    I have made a Database.mdf that contains a table that Includes 3 diffrent Column Names.

    1:"ID" This one i set to an primary key and i have also made it automaticlly put a number in order to the list. Like 1,2,3,4,5 for each data added. Data Type for this is int.
    2:"WordCombination" Were i enter my word combination that i want it to look for)
    3."TypeofCombination" Were i enter what the program should add to what i typed in texbox1 if it contains the WordCombination.

    I want my program to se if texbox1 contains any of those WordCombinations in my database. And if thats true, Then i want it to show whats in the TypeofCombination + the text in texbox1 in texbox2.

    Example:
    This is how ive made the database


    And this is how i want my program to work:

    If i enter "What do you want ?" in Textbox1 i want the program to find that "What do you want ?" contains "you want" like in ID:1 WordCombination:you want. So becuse this is ture it should enter TypeofCombination in this case "Question:" + what i entered in Textbox1 to show in Textbox2.

    So if i write "What do you want ?" and click start. Textbox2 should show "Question: What do you want ?"

    If i Write "My name is Jake" and click start. Textbox2 should show "Answer: My name is Jake"

    If I Write "hello you Hi there Jacob" and click start. Textbox2 should show "Greeting: hello you Hi there Jacob"

    Like this


    And i Should be able to add and delete Word Combinations Without having to change the program.

    Big Thanks to anyone who could help me with this.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Database Contains

    Moved From The CodeBank (which is for sharing code rather than posting questions )

  3. #3
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Re: Database Contains

    Do you have any code written out to show you've attempted it?

    Basically what you would need to do when the user clicks on start is to do do an SQL select statement to look for that word combination.

    SELECT TypeOfConnection
    FROM tblNameHere
    WHERE WordCombination = txtBox1.Text

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: Database Contains

    THis is only how far ive come and this is it not working so i have to put some other commands but i dont know which:


    I dont really know how to refer to the database properly.
    In what way can i enter these SELECT comands. Can you give me an example.

  5. #5
    Frenzied Member
    Join Date
    Jul 2004
    Posts
    1,202

    Re: Database Contains

    Quote Originally Posted by Oldphones View Post
    THis is only how far ive come and this is it not working so i have to put some other commands but i dont know which:


    I dont really know how to refer to the database properly.
    In what way can i enter these SELECT comands. Can you give me an example.
    You first need to put End If above the end sub.
    come back and mark your original post as resoved if your problem is fixed

    Jamie Garland

  6. #6
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Re: Database Contains

    Quote Originally Posted by Oldphones View Post
    I dont really know how to refer to the database properly.
    In what way can i enter these SELECT comands. Can you give me an example.

    Please see bottom of post #7 to see how to do it with entering code just in the start button



    This is what I use to connect to my access database. To connect to another, check out http://www.connectionstrings.com/
    Code:
        Public dbConn As OleDbConnection
        Private Const dbProvider As String = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        Private Const dbPath As String = "|DataDirectory|"
        Private Const dbName As String = "ServiceSetups.mdb;"
        Private Const dbUserName As String = "User ID=admin;"
        Private Const dbPassword As String = "Password=;"
    
        Public Function setDBConn() As Boolean
            'Returns True  if Connection was   Successful
            'Returns False if Connection was UnSuccessful
    
            Dim dbSource As String = String.Empty  'Includes Data Source=,path, and name
            Dim strMsg As String = String.Empty    'Error Message(if any)
    
            dbConn = New OleDbConnection
    
            dbSource = "Data Source=" & dbPath & "\" & dbName
            dbConn.ConnectionString = dbProvider & dbSource & dbUserName & dbPassword
    
            Try
                dbConn.Open()
            Catch ex As Exception
                strMsg = "Error: " & ex.Message.ToString & Environment.NewLine & _
                        Environment.NewLine & "Closing program, sorry."
                MessageBox.Show(strMsg, "Database Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                dbConn = Nothing
    
                'We weren't able to connect.
                Return False
            End Try
    
            Return True
        End Function
    This is my code for the current application I am writing. It connects to a database called ServiceSetups.mdb. I call setDBCon and if that returns True I connected successfully, False, I inform the user and close my application as my application needs to be able to connect to the database.


    Below is my code retrieving information...I put the information into a DataGridView but this is to give you an example of how to connect, retrieve, and display.
    Code:
            Dim dbCMD As New OleDb.OleDbCommand With {.Connection = dbConn, .CommandText = _
                <SQL>
                    SELECT *
                    FROM tblServices
                    WHERE ServiceDateTime
                        BETWEEN ? AND ?
                        AND IsActive = True
                    ORDER BY ServiceDateTime 
                </SQL>.Value}
            Dim pDate1 As New OleDbParameter With {.DbType = DbType.DateTime, .ParameterName = "@P1"}
            Dim pDate2 As New OleDbParameter With {.DbType = DbType.DateTime, .ParameterName = "@P2"}
    
            Dim dbReader As OleDb.OleDbDataReader
    
            pDate1.Value = mDate1
            pDate2.Value = mDate2
    
     dbCMD.Parameters.AddRange(New OleDbParameter() {pDate1, pDate2})
            Try
                dbReader = dbCMD.ExecuteReader
    
                ViewServices.Rows.Clear()
    
                ViewServices.ColumnCount = 7
                ViewServices.Columns(0).Visible = False  'ID Number of Service
                ViewServices.Columns(1).HeaderText = "Date"
                ViewServices.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                ViewServices.Columns(1).Width = 80
                ViewServices.Columns(2).HeaderText = "Time"
                ViewServices.Columns(2).Width = 80
                ViewServices.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                ViewServices.Columns(3).HeaderText = "Sent By"
                ViewServices.Columns(3).Width = 175
                ViewServices.Columns(4).HeaderText = "Brand"
                ViewServices.Columns(4).Width = 50
                ViewServices.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                ViewServices.Columns(5).HeaderText = "RA #"
                ViewServices.Columns(5).Width = 100
                ViewServices.Columns(6).HeaderText = "Assigned To"
                ViewServices.Columns(6).Width = 175
    
                Do While dbReader.Read()
                    ViewServices.Rows.Add(New String() _
                             {dbReader.Item("ServiceID").ToString, _
                              GetDateOrTime(CDate(dbReader.Item("ServiceDateTime")), False), _
                              GetDateOrTime(CDate(dbReader.Item("ServiceDateTime")), True), _
                              GetAgentName(CLng(dbReader.Item("SentByID"))), _
                              dbReader.Item("Brand").ToString, _
                              dbReader.Item("RANumber").ToString, _
                              GetAgentName(CLng(dbReader.Item("AssignedToID")))})
                Loop
                If ViewServices.RowCount > 5 Then
                    Me.Width = 700
                End If
    
                ViewServices.ClearSelection()
    
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
                Beep()
                Me.Close()
            End Try
    Last edited by TrickyNick; Jan 29th, 2012 at 05:23 AM.

  7. #7
    Addicted Member
    Join Date
    Oct 2010
    Posts
    178

    Re: Database Contains

    You code would look something like this

    vb.net Code:
    1. Dim dbCMD As New OleDb.OleDbCommand With {.Connection = dbConn, .CommandText = _
    2.             <SQL>
    3.                     SELECT TypeOfConnection
    4.                     FROM tblNameHere
    5.                     WHERE WordCombination = ?
    6.             </SQL>.Value}
    7.         Dim pWord1 As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "@P1"}
    8.  
    9.         Dim dbReader As OleDb.OleDbDataReader
    10.  
    11.         'This line sets pWord1 to TextBox1
    12.         pWord1.Value = TextBox1.Text
    13.  
    14.         'In short, this line changes the ? in the above SQL to whatever is in TextBox1
    15.         dbCMD.Parameters.Add(pWord1)
    16.  
    17.         Try
    18.             'This line actually gets the information
    19.             dbReader = dbCMD.ExecuteReader
    20.         Catch ex As Exception
    21.             'An error happened when trying to retrieve from the database
    22.             Debug.Print ex.MEssage
    23.         End Try

    Another way to do it you can do is load all your WordCombo & TypeOfCombo into a string 2 dimensional arrary then when the user clicks on start you look for the WordCombo tha way.

    EDIT2: (Put this in your start button and change names in my code to match yours.
    vb.net Code:
    1. Dim isFound As Boolean = False
    2.         Dim i As Integer = 0
    3.  
    4.         b.MoveFirst()
    5.         TextBox2.Text = String.Empty
    6.  
    7.         For i = 0 To b.Count - 1
    8.             If TextBox1.Text.Contains(DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString) Then
    9.                 isFound = True
    10.                 TextBox2.Text &= DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value.ToString & _
    11.                                 " " & TextBox1.Text & Environment.NewLine
    12.             End If
    13.             b.MoveNext()
    14.         Next
    Last edited by TrickyNick; Jan 29th, 2012 at 05:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width