I wanted to do the sql command "select DISTINCT" but it didn't work if there was another solution. I have four tables namely GSDTS, GSGTS, STFTS and TEMPTABL.
Thanks
error
Private Sub PopulateComboBox()
Dim query As String = "SELECT DISTINCT PNM FROM GSDTS"
Try
Using con As OleDbConnection = New OleDbConnection(cn)
Using sda As OleDbDataAdapter = New OleDbDataAdapter(query, con)
'Fill the DataTable with records from Table.
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
'Insert the Default Item to DataTable.
Dim row As DataRow = dt.NewRow()
row(0) = ""
dt.Rows.InsertAt(row, 0)
'Assign DataTable as DataSource
ComboBox1.DataSource = dt
ComboBox1.DisplayMember = "PNM"
ComboBox1.ValueMember = "PNM"
End Using
End Using
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
End Sub
you neeed to cnage the query to
SELECT DISTINCT PNM FROM GSDTS
UNION
SELECT DISTINCT PNM FROM GSGTS
UNION
SELECT DISTINCT PNM FROM STFTS
UNION
SELECT DISTINCT PNM FROM TEMPTAB
ORDER BY PNM
the DISINCT will only take UNIQUE OMN and the UNION will take care of all duplicates.
But you will so never know,. which PMN belongs to which table.
Related
Dear All Master,
I have 3 tables namely SALES,SALESDETAIL,CUSTOMER. I want to combine the sales table with the sales details and also in the sales table there is a field or column "CODECUS" so I want to change it to "NAMECUST" by taking that data in the CUSTOMER table based on "CODECUS".
Thanks
Dim Path As String = Environment.CurrentDirectory
Dim cn As String = "provider=Microsoft.ACE.OLEDB.12.0; data source=" & Path & "\ORDERS.accdb;"
Dim source1 As New BindingSource()
Private Sub fillDataGridView1()
Try
Dim query As String = "SELECT * FROM SALES UNION SELECT * FROM SALESDETAIL"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source1.DataSource = dt
Me.DataGridView1.DataSource = source1
Me.DataGridView1.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Dear All Master,
I have tried but there is no result from sql select sum and only appears group from the column "PNM". Is there anything wrong with the sql I created?. is there any other solution?.
I don't know why it doesn't appear in the sum value in the "BLC" column in the TEMPTABL table.
Thanks
Private Sub fillDataGridView1()
Try
Dim query As String = "SELECT PNM,NOD,QTY,CIU,DPR FROM GSDTS WHERE QTY > 0"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source1.DataSource = dt
Me.DataGridView1.DataSource = source1
Me.DataGridView1.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Private Sub fillDataGridView2()
Try
Dim query As String = "select * FROM TEMPTABL"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source2.DataSource = dt
Me.DataGridView2.DataSource = source2
Me.DataGridView2.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Sub deltemptabl()
Try
Dim sql As String = "DROP TABLE TEMPTABL"
Using conn As New OleDbConnection(cn),
cmd As New OleDbCommand(sql, conn)
conn.Open()
cmd.ExecuteNonQuery()
End Using
Catch myerror As Exception
MessageBox.Show("Error: " & myerror.Message)
End Try
End Sub
Sub temptablsum()
Try
'here is the line of code below sql select sum
Dim sql As String = "select PNM, sum((qty*ciu)*(1-dpr/100)) AS BLC INTO TEMPTABL from GSDTS group by PNM"
Using conn As New OleDbConnection(cn),
cmd As New OleDbCommand(sql, conn)
conn.Open()
cmd.ExecuteNonQuery()
End Using
Catch myerror As Exception
MessageBox.Show("Error: " & myerror.Message)
End Try
End Sub
DPR is null, therefore it will return null when you try to compute it. You can account for that with isnull(), setting it to 1 (or whatever fits the scenario) whenever the column could be null
sum((qty*ciu)*(1-ISNULL(dpr,1)/100))
Edit:
After looking over it again, you may want to apply the isnull function to the entire sum to return 0 if null. It's difficult to give exacts as I dont know what each column means or specifics to the scenario
ISNULL(sum((qty*ciu)*(1-dpr/100)),0)
Edit/option 2:
Encapsulate that column in a case statement to handle the scenario if needed.
CASE WHEN DPR IS NOT NULL THEN sum((qty*ciu)*(1-dpr/100)) ELSE ReturnSomethingElse END AS DPR
Option 3:
Change the table column and front end to not accept null values if it's not viable for the scenario
I have the following code. Please help me on how to get the last record of the records.
Dim DteQry As New OdbcCommand
DteQry.CommandText = "select distinct trn_date from order_data order by trn_date asc limit 30;"
Conn.open()
DteQry.Connection = Conn
Dim qryRdr As OdbcDataReader
qryRdr = DteQry.ExecuteReader()
If qryRdr.HasRows() Then
Dim StartDte = qryRdr.GetValue(0)
TextBox2.Text = qryRdr.GetValue(0)
Exit Sub
End If
How about this...
You cannot get the row count directly from OdbcDataReader. So just create the dataTable dt and load the dataset from reader to dt. Get the row count from datatable and then you can get last record as below.
If qryRdr.HasRows() Then
Dim dt As DataTable
dt.Load(qryRdr)
Dim StartDte = dt.Rows(dt.Rows.Count-1).item(0)
TextBox2.Text = dt.Rows(dt.Rows.Count-1).item(0)
Exit Sub
End If
dim Last_record as integer
Dim tb As DataTable
dim dp = New OleDbDataAdapter("select max(ID) from tb2", cn)
dp.Fill(tb)
If Not IsDBNull(tb.Rows(0).Item(0)) Then Last_record = tb.Rows(0).Item(0)
tb.clear
SELECT * FROM TableName WHERE id=(SELECT max(id) FROM TableName);
I have 2 tables, products, and author, the author table has a column that relates to the product table by an ID so I have done a select statement below to get the right rows based on the ID's. However the author table might not have any data in and therefore no ID to the product table. If this is the case then the information from the product doesn't show up.
So my question is how do i handle this?
Dim ID As String = Request("id")
If String.IsNullOrEmpty(ID) Then
Response.Redirect("/Default.aspx")
End If
Try
Using conn As New OleDbConnection(strcon)
conn.Open()
Dim cmd As String = "SELECT * FROM tblProducts, tblPrdAuthor " & _
"WHERE tblProducts.ID = " & ID & " AND tblPrdAuthor.paPrdID = tblProducts.ID"
Using da As New OleDbDataAdapter(cmd, conn)
Dim ds As New DataSet()
da.Fill(ds)
'Bind to the repeater
rptProduct.DataSource = ds
rptProduct.DataBind()
End Using
End Using
Catch ex As Exception
Throw ex
End Try
Thank you!
Select * from table1 as A left outer join table2 as B on (A.id=B.id) where a.id=101 and b.pid=102
How to find the largest value in a column of a access table. and displayed in a text box. I give 101 as the default value of the column and the table is empty. I try like this.. But its not working. Code is given below
Dim empid As Integer
empid=101
TXTEMPID.Text=empid
getConnect()
Dim strSQL As String = "SELECT MAX(EMP_ID) FROM EMPLOYEE "
Dim cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader
cmd = New OleDb.OleDbCommand(strSQL, Conn)
Try
Conn.Open()
Reader = cmd.ExecuteReader()
If Reader.Read Then
empid = CInt(Reader("EMP_ID"))
End If
MessageBox.Show(empid)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Close()
End Try
TXTEMPID.Text = empid + 1
If the table is empty then there is no "largest value" because the table contains no values at all.
Edit
Ah, okay. It sounds like you were tripping over the fact that, for an empty table, expressions like DMax("EMP_ID","YourTable") will return Null, and Null + 1 will return Null, so how do we get started? You could try something like...
Me.txtEMP_ID.Value = Nz(DMax("EMP_ID","YourTable"), 100) + 1
...in the Form Load event, although I should mention that this type of approach can cause problems is your database is (or will ever become) multi-user.
here is the query
Select Top 1 MAX(col 2), col1 from Table1 group by col1
to get the results you have to perform
Dim cnnOLEDB As New OleDbConnection
Dim cmdOLEDB As New OleDbCommand
Dim strConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.Environment.CurrentDirectory & "\URDataBaseName.mdb"
cnnOLEDB.ConnectionString = strConnectionString
cnnOLEDB.Open()
cmdOLEDB.CommandText ="Select Top 1 MAX(col 2), col1 from Table1 group by col1"
cmdOLEDB.Connection = cnnOLEDB
txtbox.text = cmdOLEDB.ExecuteScalar().ToString()
ExecuteScalar Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored
I change my code like this given below.. Its working coooool......
getConnect()
Conn.Open()
Dim str As String
Dim newNumber As Integer
str = "SELECT MAX(EMP_ID) AS MAXIMUM FROM EMPLOYEE"
Dim cmd As OleDbCommand = New OleDbCommand(str, Conn)
Dim dr As OleDbDataReader
dr = cmd.ExecuteReader
If dr.HasRows Then
While dr.Read()
TXTEMPID.Text = dr("MAXIMUM").ToString
newNumber = CInt(Val(TXTEMPID.Text))
If newNumber = 0 Then
newNumber = 101
TXTEMPID.Text = CStr(newNumber)
Else
newNumber = newNumber + 1
TXTEMPID.Text = CStr(newNumber)
End If
End While
End If
Conn.Close()
Thank you all for replay and comment my question