|
-
May 8th, 2020, 02:05 PM
#1
Thread Starter
Junior Member
Displaying ACCESS/VB.Net Query results WITHOUT any data Binding.
Hi, guys. Hope you are all keeping safe and well in this awful time.
I am having time to follow earlier lines of thought, mainly inspired by JMC (thanks)
For various reasons, I am constrained to using VB.NET to 'manipulate' a ".accdb" type Access database.
I have a 90% working database with which I am quite pleased - again, in a large part to you guys offering advice.
Could you help once again with my final (Ihope!) problem, please?
As you will see from the code below, I call sql for various items, and they all work very well : except the "Search".
It may well be working, but I cannot interpret the results, because I don't know how to access them. (No pun intended).
Code:
Public Class Form1
Dim ds As New DataSet 'HOLDS A DATASET OBJECT
Dim da As OleDb.OleDbDataAdapter 'HOLDS A DATA_ADAPTER OBJECT
Dim sql As String 'HOLDS A SQL STRING
Dim con As New OleDb.OleDbConnection 'THE CONNECTION OBJECT
Dim dbProvider As String 'HOLDS THE PROVIDER
Dim dbSource As String 'HOLDS THE DATA SOURCE
Dim TheDatabase As String 'HOLDS THE DATABASE NAME
Dim THEDATABASEFOLDER As String 'HOLDS THE DATABASE NAME
Dim FullDatabasePath As String 'HOLDS THE DATABASE PATH
Dim status As String 'THIS SHOWS THE TYPE OF OPERATION, ADD NEW, DELETE, SEARCH & etc.
Dim curRec, curCell As Integer 'POINTS TO CURRENT RECORD ROW, CURRENT COLUMN
Dim maxRec As Integer 'HOLDS THE CURRENT NUMBER OF RECORDS IN THE TABLE
'**********
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
'START THE INTERRUPT TIMER TO UPDATE THE TOOLSTRIP
Timer1.Enabled = True
Timer1.Start()
'SET UP THE PROVIDER
dbProvider = "Provider=Microsoft.Ace.OLEDB.12.0;"
'SET UP THE DATABASE AND ITS LOCATION "C:\Users\Public\Documents"
TheDatabase = "/DB0506.accdb"
THEDATABASEFOLDER = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)
FullDatabasePath = THEDATABASEFOLDER & TheDatabase
'SET THE DATA SOURCE
dbSource = "Data Source = " & FullDatabasePath
'SET THE CONNECTION STRING
con.ConnectionString = dbProvider & dbSource
'OPEN THE DATABASE
con.Open()
'STORE AN SQL STRING
sql = "SELECT * FROM Data"
'PASS THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
da = New OleDb.OleDbDataAdapter(sql, con)
'FILL THE DATASET WITH RECORDS VIA THE DATA_ADAPTER FROM THE DATABASE (TABLE DEFINED IN SQL)
da.Fill(ds, "DB0506")
MessageBox.Show("Db Opened")
con.Close()
MessageBox.Show("Db Closed")
'TOTAL NUMBER OF RECORD ENTRIES
maxRec = ds.Tables("DB0506").Rows.Count
'POINT TO ROWINDEX OF FIRST RECORD ENTRY
curRec = 0
'DISPLAY THE RECORD IN THE TEXT BOXES
Call showRecord(curRec)
End Sub
'**********
''' <summary>
''' THIS DISPLAYS THE CURRENT RECORD IN THE TEXT BOXES, USING <c> curRow </c> as the Rowindex
``` ONLY USING THE FIRST 'ID' AND THE NEXT TWO COLUMNS AT THIS TIME
''' </summary>
Private Sub showRecord(curRec As Integer)
txtID.Text=ds.Tables("DB0506").Rows(0).Item(0)
txtFirstName.Text = ds.Tables("DB0506").Rows(curRec).Item(1)
txtLastName.Text = ds.Tables("DB0506").Rows(curRec).Item(2)
End Sub
'**********
''' <summary>
''' FIRSTNAME TEXT BOX KEYPRESS DETECTED
''' IF ADDING NEW RECORD, POINT TO NEXT TEXTBOX
''' IF SEARCHING ON COLUMN NAME "FIRSTNAME" THEN SQL THE SEARCH
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub txtFirstName_KeyUp(sender As Object, e As KeyEventArgs) Handles txtFirstName.KeyUp
If e.KeyCode = Keys.Enter Then
If status = "Add" Then
Try
txtLastName.Focus()
Catch
MessageBox.Show("Error : " - No status, Or LastName IsNot Focused")
End Try
ElseIf status = "Search" Then
Try
sql = "SELECT * FROM Data WHERE Data.FirstName Like '%" & Me.txtFirstName.Text & "%'"
'PASS THE SQL STRING AND CONNECTION OBJECT TO THE DATA_ADAPTER
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Db0506")
Catch
MessageBox.Show("Error with sql / da ??")
End Try
'
'
' WHAT DO I DO HERE TO DISPLAY THE FIRST 'FOUND' ROW DATA?????
'
'
End If
End If
End Sub
The desired result from the query may well be in the dataset, but I cannot get to it. The main reason is that, due to other considerations stipulated by my colleagues, the text boxes
showing the data cannot be Bound, nor is there room on screen for a DataGridView. Opening another window for a DGV is also being frowned upon.
Could some kind person please let me know how to find the Row Number (in the main database) where the returned search query lies? Then I can access the row,
and get and display the record contents.
Thanking you in advance,
Best regards,
Pete
Tags for this Thread
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
|