|
VB6Q I have 5 buttons. clicking them once changes that button's color to, say orange. And when another button is clicked that will become orange and the previous will reset to original color
So i want to toggle their color's depending on which one is clicked.
I want code to be efficient. No repeating codes.
Any ideas? Just tell me some ideas how to go about implementing it.
my idea:
send all button click events to a change color function. but we need to know which button has been clicked
Any other ideas welcom
|
|
|
|
|
ssonby wrote: send all button click events to a change color function. but we need to know which button has been clicked
Correct: you should just pass a reference to the clicked button.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
ya how will i pass a refernce. its VB6. how do i come to know which button clicked it
|
|
|
|
|
Each button has it's own click event. In that event change the current button to orange and then set all the other buttons to the original color. Pretty simple actually.
EDIT:
Sorry, didn't see the part about no duplicate code. What you can do is create a control array out of the buttons then just reference the index of the button clicked. Also simple.-------------------------------------
Do not do what has already been done.
Absolute power corrupts absolutely.. but it ROCKS absolutely, too.
|
|
|
|
|
I knew that. But I dont want to keep it as array. as i can't give it my own name to each. it becomes a boring name. Is there a way to know who clicked it. like a 'caller' class or 'sender'. In .NET we have that 'e' variable which can references to the current button clicked
Thanks for your responses
|
|
|
|
|
About the only way to make it less generic is to create constant variables for each index so they're more descriptive. I'm not aware of any kind of 'sender' functionality. I guess you do what you can with the tools you're given. -------------------------------------
Do not do what has already been done.
Absolute power corrupts absolutely.. but it ROCKS absolutely, too.
|
|
|
|
|
and abandon doing this in VB6. It's hasn't been supported by MS in quite some time.
Move up to VB.NET at least.
|
|
|
|
|
Good day,
What is the difference between these methods of updating and which is better:
1. Add new row to datatable
Dim dr As DataRow = dt.NewRow
dr.Item("OrderStatus") = "OPEN"
dt.Rows.Add(dr)
'Update new row to database
Dim qry As String = "SELECT * FROM Table"
Conn.Open()
Dim da1 As New SqlClient.SqlDataAdapter
da1.SelectCommand = New SqlClient.SqlCommand(qry, MAIN.data.Conn)
Dim cb1 As SqlClient.SqlCommandBuilder = New System.Data.SqlClient.SqlCommandBuilder(da1)
da1.Update(MAIN.data.DS, "Table")
conn.close
2. Execute non query - does this work as stored procedure?
Dim strSQL As String = "INSERT INTO Table (OrderStatus)VALUES (@OrderStatus)
Dim cmdAddNew As New SqlClient.SqlCommand(strSQL, Conn)
cmdAddNew.Parameters.Add("@ScanStatus", SqlDbType.Varchar,50).Value = "OPEN"
Conn.Open()
cmdAddNew.ExecuteNonQuery()
Conn.Close()
|
|
|
|
|
I would say it all depends on the situaton. If you have a DataGrid bound to a DataTable, and your user adds a record through the DataGrid, then the first instance would probably be better. If in your data Layer you need a function to Save an object then I would use the second option. In the second option a stored procedure would definitely work. You need to set strSQl to the name of your Stored procedure, and then add
cmdAddNew.CommandType = CommandType.StoredProcedure .
|
|
|
|
|
Hi mates,
How can I join an access table with sql server table using vb.net. In access form, I can use the link table and by creating new query I can drag and drop the sql table and access table. How will I do that in vb.net. I tried dataset but the connection is only for access or sql server. Currently I have two database in my program. One is mdb and other is sql server. Please help me. Thanks in advance.C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
C#Coudou wrote: How can I join an access table with sql server table using vb.net
You can't, what you need to do is get both tables into the business layer (VB), using their respective connections, and then use a Linq query to do it. I hate linq it is one of the greatest disappointments I have run across but in this instance it is the tool for the job.
I have found this link[^] useful to my great disgust.Never underestimate the power of human stupidity
RAH
|
|
|
|
|
thank you for your suggestion. i read the link but unfortunately linq is for only select statement. i need to update, insert, delete access tables which is one of the column is equal to sql. Heres my sample query.
Where: W_Analysis (access mdb) and OrderMaster/ChemMaster (sql database)
Insert into W_Analysis(OrderNo,ItemCode,ItemName,ChemCode,ChemName)
Select OrderMaster.OrderNo,OrderMaster.ItemCode,OrderMaster.ItemName, ChemMaster.ChemCode, ChemMaster.ChemName
From OrderMaster Inner Join ChemMaster On OrderMaster.ItemCode = ChemMaster.ChemCode
Where ChemMaster.ChemCode = 1159C# コードMicrosoft End User
2000-2008
「「「「「「「「「「「「「「「「「「「「「「「「「「「「
The best things in life are free
」」」」」」」」」」」」」」」」」」」」」」」」」」」」
|
|
|
|
|
I have a report project where a timer is polling the DB for a user abort. Once the abort is discovered I want to call a procedure on the master thread interrupting it so that it can abort the report. This way I don't have to get checking for an abort in the master thread.
I tried using delegates and the invoke method hoping that it would work like the form.invoke which calls the procedure in the form's thread. But it didn't work. Anyone know how to do this without using forms?
Here is a simple example:
Delegate Sub AbortDelegate()
Public Class ReportProcesser
Private _reportThreadID As Integer
Friend Abort As New AbortDelegate(AddressOf AbortReport)
Public WithEvents CheckForAbortTimer As New Timers.Timer(500)
Public ReadOnly Property ManagedThreadID() As Integer
Get
Return _reportThreadID
End Get
End Property
Public Sub New()
_reportThreadID = Threading.Thread.CurrentThread.ManagedThreadId
End Sub
Public Sub AbortReport()
MsgBox("Report Thread ID: " & Me.ManagedThreadID & vbCrLf & _
"Abort Report On Thread ID: " & Threading.Thread.CurrentThread.ManagedThreadId & vbCrLf)
End Sub
Private Sub CheckForAbortTimer_Elapsed() Handles CheckForAbortTimer.Elapsed
Static alreadyProcessing As Boolean = False
If Not alreadyProcessing Then
alreadyProcessing = True
Me.Abort.Invoke()
alreadyProcessing = False
End If
End Sub
End Class
Module Main
Sub main()
Dim report As New ReportProcesser
report.CheckForAbortTimer.Start()
Dim startTime As Date = Now
Do Until DateDiff(DateInterval.Minute, startTime, Now) >= 5
'end in 5 min or when stopped from Visual studio
Loop
End Sub
End Module
Thanks.
|
|
|
|
|
Hi all.
I want to know that how can I identify if a file is an application or a setup by vb.net programming with the extension ".exe".
Suggest me some ways.
Thanks.
Gagan
|
|
|
|
|
There's no way to tell the difference. An .exe is an .exe is an .exe. There's no way to tell what that .exe does.
|
|
|
|
|
Okay. Thanks for your suggestion.
Gagan
|
|
|
|
|
setup exe's do not live on their own, at least, not .Net ones. There are ways to package them as a single exe, however the setup.exe generated by .net deployment projects is just a stub calling the msi in the same folder. Most standalone exe installers are some form of self extracting archive, so there should be headers in that type of file that can be read. if you unpack those files you will find msi files, and maybe cab files inside.
|
|
|
|
|
Good day,
I have tried various methods to update an SQL database, but every time the database is updated but not the dataset.
I have done it with addrow and accept changes and with execute no query, but the dataset does not register the changes made. I have to MAIN.data.DS.Tables.Remove("Orders") and then add the datatable back to the dataset with the data adapter:
Example 1:
Function Add_Orders_Row() As Boolean
Try
Dim RowISAdded As Boolean = False
If txtQuantity.Text <> "" And txtProductionNo.Text <> "" _
And txtWeek.Text <> "" And txtPart.Text <> "" _
And cboProductCode.Text <> "" And txtPart.Text <> "" _
And txtWorksOrderNo.Text <> "" Then
Dim dt As DataTable = MAIN.data.DS.Tables("Orders")
Dim dr As DataRow = dt.NewRow
dr.Item("OrderStatus") = cboOrderStatus.Text
dr.Item("SamsungRef") = "NA"
dr.Item("ProductCode") = cboProductCode.Text
dr.Item("ProductSerialNo") = txtProductSerialNo.Text
dr.Item("ProductionNo") = txtProductionNo.Text
dr.Item("WeekNo") = txtWeek.Text
dr.Item("PartNo") = CType(txtPart.Text, Integer)
dr.Item("OrderDate") = dtpOrderDate.Text
dr.Item("QuantityOrder") = CType(txtQuantity.Text, Integer)
dr.Item("QuantityScanned") = 0
dr.Item("WorksOrderNo") = txtWorksOrderNo.Text
If txtFirstSerial.Text = "" Then
dr.Item("FirstSerialNo") = "<SCAN>"
dr.Item("LastSerialNo") = "<CALC>"
Else
dr.Item("FirstSerialNo") = txtFirstSerial.Text
dr.Item("LastSerialNo") = txtLastSerial.Text
End If
dr.Item("timenow") = Date.Now
dt.Rows.Add(dr)
'Update new row to database
Dim qry As String = "SELECT * FROM Orders"
If MAIN.data.Conn.State = ConnectionState.Closed Then MAIN.data.Conn.Open()
Dim da1 As New SqlClient.SqlDataAdapter
da1.SelectCommand = New SqlClient.SqlCommand(qry, MAIN.data.Conn)
Dim cb1 As SqlClient.SqlCommandBuilder = New System.Data.SqlClient.SqlCommandBuilder(da1)
da1.Update(MAIN.data.DS, "Orders")
MAIN.data.DS.Tables.Remove("Orders")
'Reatach datatable to dataset. Refresh primary keys
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
Dim findrow As DataRow() = dt.Select("ProductionNo= '" & txtProductionNo.Text & "'")
If findrow.Length = 1 Then
RowISAdded = True
Else
RowISAdded = False
End If
Else
MessageBox.Show("Cannot add record, as some fields are blank. Please add all relevant data.")
RowISAdded = False
End If
Return RowISAdded
Catch ex As Exception
MessageBox.Show("Add_Orders_Row, " & ex.Message)
Return False
End Try
End Function
Example 2:
Function UNIT_Add_New() As Boolean
Try
Dim UnitIsAdded As Boolean = False
Dim strSQL As String = "INSERT INTO UNITS " & _
"(ScanCount,OrderID,SerialNo,ScanStatus,Description,ScanDate, " & _
"SamsungRef, ProductCode,ProductSerialNo,ProductionNo,WeekNo," & _
"PartNo,OrderDate,QuantityOrder,FirstSerialNo,LastSerialNo,WorksOrderNo)" & _
"VALUES" & _
"(@ScanCount,@OrderID,@SerialNo,@ScanStatus,@Description,@ScanDate,@SamsungRef, " & _
"@Productcode,@ProductSerialNo,@ProductionNo,@WeekNo,@PartNo,@OrderDate,@QuantityOrder, " & _
"@FirstSerialNo,@LastSerialNo,@WorksOrderNo); "
Dim da As New SqlClient.SqlDataAdapter
da.SelectCommand = New SqlClient.SqlCommand("SELECT * FROM UNTIS", MAIN.data.Conn)
Dim cmdAddNew As New SqlClient.SqlCommand(strSQL, MAIN.data.Conn)
cmdAddNew.Parameters.Add("@OrderID", SqlDbType.Int).Value = Me.drFoundOrder.Item("OrderID")
cmdAddNew.Parameters.Add("@ScanCount", SqlDbType.Int).Value = Units_Scanned()
cmdAddNew.Parameters.Add("@SerialNo", SqlDbType.VarChar, 50).Value = txtSerialNo.Text
cmdAddNew.Parameters.Add("@ScanStatus", SqlDbType.VarChar, 50).Value = txtScanStatus.Text
cmdAddNew.Parameters.Add("@Description", SqlDbType.VarChar, 255).Value = txtDescription.Text
cmdAddNew.Parameters.Add("@ScanDate", SqlDbType.DateTime).Value = Date.Now
cmdAddNew.Parameters.Add("@SamsungRef", SqlDbType.VarChar, 50).Value = "NA"
cmdAddNew.Parameters.Add("@ProductCode", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("ProductCode")
cmdAddNew.Parameters.Add("@ProductSerialNo", SqlDbType.VarChar, 50).Value = Me.drOrder.Item("ProductSerialNo")
cmdAddNew.Parameters.Add("@ProductionNo", SqlDbType.VarChar, 50).Value = Me.drOrder.Item("ProductionNo")
cmdAddNew.Parameters.Add("@WeekNo", SqlDbType.VarChar, 3).Value = Me.drOrder.Item("WeekNo")
cmdAddNew.Parameters.Add("@PartNo", SqlDbType.SmallInt).Value = CType(Me.drOrder.Item("PartNo"), Integer)
cmdAddNew.Parameters.Add("@OrderDate", SqlDbType.DateTime).Value = Me.drFoundOrder.Item("OrderDate")
cmdAddNew.Parameters.Add("@QuantityOrder", SqlDbType.SmallInt).Value = CType(Me.drFoundOrder.Item("QuantityOrder"), Integer)
cmdAddNew.Parameters.Add("@FirstSerialNo", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("FirstSerialNo")
cmdAddNew.Parameters.Add("@LastSerialNo", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("LastSerialNo")
cmdAddNew.Parameters.Add("@WorksOrderNo", SqlDbType.VarChar, 50).Value = Me.drFoundOrder.Item("WorksOrderNo")
If MAIN.data.Conn.State = ConnectionState.Closed Then MAIN.data.Conn.Open()
Dim itest As Integer = cmdAddNew.ExecuteNonQuery()
'da.InsertCommand = cmdAddNew
'da.Update(MAIN.data.DS, "UNITS")
MAIN.data.DS.Tables("UNITS").AcceptChanges()
If MAIN.data.Conn.State = ConnectionState.Open Then MAIN.data.Conn.Close()
If itest = 1 Then
UnitIsAdded = True
Else
UnitIsAdded = False
End If
Return UnitIsAdded
Catch ex As Exception
MessageBox.Show("UNIT_Add_New, " & ex.Message)
Return False
End Try
End Function
Both do the same that SQL database is updated, but the dataset does not know what happened. So you cannot count the rows or get data from the new row in the dataset.
|
|
|
|
|
Read up on what AcceptChanges does first.
By calling that, you're telling the dataset that all changes that need to be written out the database HAVE been written out to the database, even if you didn't call Update first.
|
|
|
|
|
Hi,
Thanks for your reply.
I only added the accept changes later when I noticed that the ADO.net dataset-datatable is not updating. The SQL database contains the new data that was added, but the change is not reflected back to the Dataset-datatable.
This is what I am doing now to update the data set
Function Load_Table(ByVal SQLString As String, ByVal TableName As String) As Boolean
Dim SQLstr As String
Dim tableISadded As Boolean = False
Try
'open connection
If SQLcon.State = ConnectionState.Closed Then
SQLcon.Open()
End If
SQLstr = SQLString
Dim cmd As SqlCommand = New SqlCommand(SQLstr, SQLcon)
Dim da As New SqlDataAdapter(SQLstr, SQLcon)
da.Fill(pDS, TableName)
da.FillSchema(pDS, SchemaType.Mapped)
For Each dt As DataTable In pDS.Tables
If dt.TableName = TableName Then
tableISadded = True
Else
tableISadded = False
End If
Next
Return tableISadded
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
Finally
'close connection
SQLcon.Close()
End Try
End Function
'Remove the orders table
MAIN.data.DS.Tables.Remove("Orders")
''Reload datatable to dataset.
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
|
|
|
|
|
You're not saying what pDS is. You're also not showing what happens to pDS before Load_Table gets called.
|
|
|
|
|
Hi,
Hope this helps.
Datalayer class
Dim pDS As New DataSet
Public Property DS() As DataSet
Get
Return pDS
End Get
Set(ByVal value As DataSet)
pDS = value
End Set
End Property
Sub MakeConnect()
SQLcon.ConnectionString = "Data Source=192.168.10.24\BDMS;" & _
"Initial Catalog=FLATPAN;" & _
"User ID=flatpan;" & _
"Password=*******"
Try
SQLcon.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Function Load_Table(ByVal SQLString As String, ByVal TableName As String) As Boolean
Dim SQLstr As String
Dim tableISadded As Boolean = False
Try
'open connection
If SQLcon.State = ConnectionState.Closed Then
SQLcon.Open()
End If
SQLstr = SQLString
Dim cmd As SqlCommand = New SqlCommand(SQLstr, SQLcon)
Dim da As New SqlDataAdapter(SQLstr, SQLcon)
da.Fill(pDS, TableName)
da.FillSchema(pDS, SchemaType.Mapped)
For Each dt As DataTable In pDS.Tables
If dt.TableName = TableName Then
tableISadded = True
Else
tableISadded = False
End If
Next
Return tableISadded
Catch ex As Exception
MessageBox.Show(ex.Message)
Return False
Finally
'close connection
SQLcon.Close()
End Try
On the orders form call the Load_Table function to add the table to the dataset. This works.
Private Sub Orders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Load the orders table and lu products table
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders") 'THIS IS THE DA FILL FOR ORDERS
MAIN.data.Load_Table("SELECT * FROM LU_Products", "LU_Products")
MAIN.data.Load_Table("SELECT * FROM Status_Orders", "Status_Orders")
Calculate_Total_Rows(0, "LOAD") 'SOME FORM ACTIONS
Fields_Load(rowCounter) 'NOT PART OF UPDATE ONLY TO VIEW DATA
Fields_State("LOCK") 'LOCK FIELDS SO USER CANT CHANGE
End Sub
Here add a new row to the ORDERS table. This works, but cannot see the changes to the database until I remove the datatable from the dataset and then fill it back again. I think this would be very noisy as we scan barcodes into a table that will become very large over time.
Function Add_Orders_Row() As Boolean
Try
Dim RowISAdded As Boolean = False
If txtQuantity.Text <> "" And txtProductionNo.Text <> "" _
And txtWeek.Text <> "" And txtPart.Text <> "" _
And cboProductCode.Text <> "" And txtPart.Text <> "" _
And txtWorksOrderNo.Text <> "" Then
Dim dt As DataTable = MAIN.data.DS.Tables("Orders")
Dim dr As DataRow = dt.NewRow
dr.Item("OrderStatus") = cboOrderStatus.Text
dr.Item("SamsungRef") = "NA"
dr.Item("ProductCode") = cboProductCode.Text
dr.Item("ProductSerialNo") = txtProductSerialNo.Text
dr.Item("ProductionNo") = txtProductionNo.Text
dr.Item("WeekNo") = txtWeek.Text
dr.Item("PartNo") = CType(txtPart.Text, Integer)
dr.Item("OrderDate") = dtpOrderDate.Text
dr.Item("QuantityOrder") = CType(txtQuantity.Text, Integer)
dr.Item("QuantityScanned") = 0
dr.Item("WorksOrderNo") = txtWorksOrderNo.Text
If txtFirstSerial.Text = "" Then
dr.Item("FirstSerialNo") = "<SCAN>"
dr.Item("LastSerialNo") = "<CALC>"
Else
dr.Item("FirstSerialNo") = txtFirstSerial.Text
dr.Item("LastSerialNo") = txtLastSerial.Text
End If
dr.Item("timenow") = Date.Now
dt.Rows.Add(dr)
'Update new row to database
Dim qry As String = "SELECT * FROM Orders"
If MAIN.data.Conn.State = ConnectionState.Closed Then MAIN.data.Conn.Open()
Dim da1 As New SqlClient.SqlDataAdapter
da1.SelectCommand = New SqlClient.SqlCommand(qry, MAIN.data.Conn)
Dim cb1 As SqlClient.SqlCommandBuilder = New System.Data.SqlClient.SqlCommandBuilder(da1)
da1.Update(MAIN.data.DS, "Orders")
'REMOVE THE DATABLE
MAIN.data.DS.Tables.Remove("Orders")
'Reatach datatable to dataset. Refresh primary keys
MAIN.data.Load_Table("SELECT * FROM Orders", "Orders")
'MAKE SURE UPDATE MADE
Dim findrow As DataRow() = dt.Select("ProductionNo= '" & txtProductionNo.Text & "'")
If findrow.Length = 1 Then
RowISAdded = True
Else
RowISAdded = False
End If
Else
RowISAdded = False
End If
Return RowISAdded
Catch ex As Exception
MessageBox.Show("Add_Orders_Row, " & ex.Message)
Return False
End Try
End Function
|
|
|
|
|
This is hard to follow. Why are you apparently keeping the entire database, or at least entire tables, in memory? This is a design that will offer a performance gain for a small amount of time. The machine will eventually start swapping your app out to disk, slowing performance down dramatically.
|
|
|
|
|
Hmm...ok, my first reply may not be your problem. I may have misunderstood your problem. Let me see if I understand now.
In the first example, I still believe it's because you haven't set up the TableMapping . Though, I have to say, your code is a little hard to read and I believe a little non-standard.
As to the second example, TableMapping isn't your issue because you're running ExecuteNonQuery . That section of code should update the database, but it won't update your DataTable in memory. You have to understand the basics of ADO.Net. When the DataTable is created, it basically makes a copy of the database in memory. It is not linked at all to the underlying Database. That's why you have to run the Update. If the TableMapping is set up properly, then, when the Update is called, it tells the DataAdapter to find the matching Table in the matching DataSet and update the cells based on their RowState property. If the RowState indicated Insert, then the SQL Insert is called. If the RowState says Modify, then it runs the SQL Update. If it says Delete, then it runs the SQL Delete.
But, there is no static link between your DataTable and the DataSet. When you run the SQL Insert, you also will have to refresh your DataTable, or insert the data into the DataTable as well.
|
|
|
|
|
If you're using ADO.Net, I believe you have to set up the TableMapping before the Update will work.
See: Table Mapping in ADO.Net[^]
|
|
|
|
|