-
Jan 27th, 2012, 06:28 PM
#1
Thread Starter
New Member
-
Jan 27th, 2012, 07:33 PM
#2
Re: Database Contains
Moved From The CodeBank (which is for sharing code rather than posting questions )
-
Jan 28th, 2012, 06:06 AM
#3
Addicted Member
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
-
Jan 28th, 2012, 12:01 PM
#4
Thread Starter
New Member
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.
-
Jan 28th, 2012, 07:05 PM
#5
Frenzied Member
Re: Database Contains
Originally Posted by Oldphones
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
-
Jan 29th, 2012, 04:00 AM
#6
Addicted Member
Re: Database Contains
Originally Posted by Oldphones
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.
-
Jan 29th, 2012, 04:13 AM
#7
Addicted Member
Re: Database Contains
You code would look something like this
vb.net Code:
Dim dbCMD As New OleDb.OleDbCommand With {.Connection = dbConn, .CommandText = _ <SQL> SELECT TypeOfConnection FROM tblNameHere WHERE WordCombination = ? </SQL>.Value} Dim pWord1 As New OleDbParameter With {.DbType = DbType.String, .ParameterName = "@P1"} Dim dbReader As OleDb.OleDbDataReader 'This line sets pWord1 to TextBox1 pWord1.Value = TextBox1.Text 'In short, this line changes the ? in the above SQL to whatever is in TextBox1 dbCMD.Parameters.Add(pWord1) Try 'This line actually gets the information dbReader = dbCMD.ExecuteReader Catch ex As Exception 'An error happened when trying to retrieve from the database Debug.Print ex.MEssage 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:
Dim isFound As Boolean = False Dim i As Integer = 0 b.MoveFirst() TextBox2.Text = String.Empty For i = 0 To b.Count - 1 If TextBox1.Text.Contains(DataGridView1.Item(1, DataGridView1.CurrentRow.Index).Value.ToString) Then isFound = True TextBox2.Text &= DataGridView1.Item(2, DataGridView1.CurrentRow.Index).Value.ToString & _ " " & TextBox1.Text & Environment.NewLine End If b.MoveNext() 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|