|
jkirkerx wrote:
If(item.FCOST <> 0 And item.FSHIPQTY <> 0, item.FCOST * item.FSHIPQTY, 0) If FCOST or FSHIPQTY is equal to 0 , then FCOST * FSHIPQTY will return 0 . There's no need for the conditional here, or any of the similar conditionals.
jkirkerx wrote:
Select Case True
Case item.FPRICE.Equals(0)
pI.FExtMargin -= item.FCOST * item.FSHIPQTY
Case Else
pI.FExtMargin += (item.FCOST - item.FPRICE) * item.FSHIPQTY
End Select If FPRICE is equal to 0 , then FCOST * FPRICE is equal to FCOST . Both cases are trying to perform the same calculation, so the Select Case is not required.
I say "trying to", because you're actually inflating the margin. For each item in the invoice which has a price, you're adding the cumulative difference between the price and the cost, rather than the difference between the price and the cost of that item.
Eg:
Items:
------
1: Price = 10; Cost = 5; Qty = 1
2: Price = 10; Cost = 5; Qty = 1
3: Price = 10; Cost = 5; Qty = 1
Your calculation:
-----------------
1: FExtPrice = 10; FExtCost = 5; FExtMargin = 0 + 10 - 5 = 5
2: FExtPrice = 20; FExtCost = 10; FExtMargin = 5 + 20 - 10 = 15
3: FExtPrice = 30; FExtCost = 15; FExtMargin = 15 + 30 - 15 = 30
Total margin: 30 = 100% !
Correct calculation:
--------------------
1: FExtPrice = 10; FExtCost = 5; FExtMargin = 0 + 10 - 5 = 5
2: FExtPrice = 20; FExtCost = 10; FExtMargin = 5 + 10 - 5 = 10
3: FExtPrice = 30; FExtCost = 15; FExtMargin = 10 + 10 - 5 = 15
Total margin: 15 = 50%
This code should produce the correct results in a single statement, without needing to manually loop over the items:
Dim gInvoicesAll As List(Of MarginCustomerInvoicesR1) = cTpAll.OrderBy(Function(ob) ob.FINVNO).GroupBy(Function(inv) inv.FINVNO).Select(Function(cl) New MarginCustomerInvoicesR1() With {
.FCustNo = cl.First().FCUSTNO,
.FInvNo = cl.First().FINVNO,
.FShipDate = cl.First().FSHIPDATE,
.FDescript = cl.First().FDESCRIPT,
.FAmount = cl.Sum(Function(item) item.FAMOUNT),
.FExtCost = cl.Sum(Function(item) item.FCOST * item.FSHIPQTY),
.FExtPrice = cl.Sum(Function(item) item.FPRICE * item.FSHIPQTY),
.FExtMargin = cl.Sum(Function(item) (item.FPRICE - item.FCOST) * item.FSHIPQTY),
.FPercent = If(cl.Sum(Function(item) item.FCOST * item.FSHIPQTY) = 0, 0, Math.Round(cl.Sum(Function(item) (item.FPRICE - item.FCOST) * item.FSHIPQTY) / cl.Sum(Function(item) item.FCOST * item.FSHIPQTY), 4))
}).ToList() If it doesn't, then give me an example where it goes wrong.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Your case is solid here.
I'll do a case study here with this new info and facts, and fact check both methods and see what the totals are.
I really didn't think that group statement would iterate the items on the invoice and group it.
Let me see what happens ... Give me a day.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I went ahead and tested that code and it works good. I did the fact or math test and the numbers are spot on now. Don't know why I doubted the first revision of my code, which was close to what you worked on. Guess my math was off and I blamed it on the group function.
Thanks for sticking with it, and proving that your suggestion did the same thing, while being smaller and more compact.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Hi Team,
I have an Excel file that has data and a table in SQL server.
I am doing it by excel VBA code in "Audit Query Report" Consolidation.
Hence, I want to do a Consolidated report by all Users in the SQL Table. Each User data to Update\Insert in DB once click the macro button in excel, Even I existing data to replaced, New Data should add in DB.
I googled it, unfortunately, there is no related code as my per requirement, Kindly advise best way code to make a consolidation DB, am finance Background, am not much familiar SQL code through VBA.
|
|
|
|
|
Member 14784416 wrote: there is no related code as my per requirement, Actually, there are probably millions of related codes out there on the internet. But you can't search for that whole task. You have to break it into pieces and look for code examples of the pieces.
But it's hard to understand exactly what you are asking. It sounds like you just need to be able to save data from excel into sql using vba. Here is one example, sql server - Export Excel to SQL using VBA - Stack Overflow[^]
Member 14784416 wrote: am not much familiar SQL code through VBA. If you do not know how to code then you need to hire someone. There is likely no complete solution that you can download based on your requirements, it will have to be built.
Social Media - A platform that makes it easier for the crazies to find each other.
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
Thanks for your reply, ok i will do my own part, if any error,let you know
|
|
|
|
|
I have a richtextbox named
input RTB that I want to associate
its event input RTB.ChangedText to a handler programmatically.
and then it want to use
addhandller and removehandller
to enable the event and to disable it.
can someone help me to achieve
this.
Thank you very much.
|
|
|
|
|
|
Thank you for the response.
before I post my question I browsed Microsoft doc and followed its but that worked
partially .
I succeed to addhandler and it works.
but the removehandler was failed
and the event continue to work.
what I want to achieve is
when my app get input from the user I want
that myevethandler work and
when my app do processing
I want that myeventhandler do
not work.
Again thank you very much
|
|
|
|
|
Sorry, but we cannot guess what you have done wrong. The documentation explains how to use both AddHandler and RemoveHandler . You need to provide full details of what you are doing and why it is not working.
|
|
|
|
|
Hi Richard MacCutchan
This is what I done
Public Class myForm
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
' InputRTB is a richtextbox in myForm
AddHandler InputRTB.TextChanged, AddressOf NewInputTextChanged
End Sub
' slTextBox is a textbox in myForm
Private Sub NewInputTextChanged ' my event handler
slTextBox.Text = InputRTB.Text.Length.ToString
End Sub
' DoButton is a Button in myForm
Private Sub DoButton_Click(sender As Object, e As EventArgs) Handles DoButton.Click
RemoveHandler InputRTB.TextChanged, AddressOf NewInputTextChanged ' disable eventhandler
DoProcess()
AddHandler InputRTB.TextChanged, AddressOf NewInputTextChanged ' enable eventhandler
End Sub
. . .
End Class
Thank you very much
|
|
|
|
|
Look at your code in the button click event, it does the following:
1. Removes the handler
2. Calls some method
3. Adds the handler back again
RemoveHandler InputRTB.TextChanged, AddressOf NewInputTextChanged
DoProcess()
AddHandler InputRTB.TextChanged, AddressOf NewInputTextChanged
So as soon as this event is complete you have the handler catching every TextChanged event again.
|
|
|
|
|
its ok after the method is finished.
but the problem is inside the method that had access the inputRTB
textbox and cause interference
because of the event continue
to work a d that seams as removehandler called before the
method not working.
I'm thank you very much
for your efforts to help me.
|
|
|
|
|
Sorry, but I don't understand. And I have no idea what the DoProcess method is doing.
|
|
|
|
|
DoProcess manipulate the text of inputRTB and because the event still work
that cause DoProcess to be hanging.
When I run DoProcess without these addhandler removehandler events
DoProcess run smoothly and quickly.
But with them its hang.
again I'm very thank you.
|
|
|
|
|
|
OK, I have got it working (I think). You need the following changes to your code:
Dim handler As EventHandler
Public Sub New()
MyBase.New()
InitializeComponent()
handler = AddressOf NewInputTextChanged
AddHandler InputRTB.TextChanged, handler
End Sub
Private Sub NewInputTextChanged()
slTextBox.Text = InputRTB.Text.Length.ToString
End Sub
Private Sub DoButton_Click(sender As Object, e As EventArgs) Handles DoButton.Click
RemoveHandler InputRTB.TextChanged, handler
DoProcess()
AddHandler InputRTB.TextChanged, handler
End Sub
|
|
|
|
|
Hi Richard MacCutchan
Bravo !
You did it.
I'm very very Thank You. !!!
|
|
|
|
|
It was not easy to find, since your original code compiled correctly. It was only when I noticed a warning in Visual Studio that I was able to find the answer.
|
|
|
|
|
I was afraid that you give up
|
|
|
|
|
Sometimes I do, sometimes I don't. Your lucky day today; unless you are self-isolating from COVID-19.
|
|
|
|
|
|
Member 13569650 wrote: but the removehandler was failed
and the event continue to work.
Define "failed".
|
|
|
|
|
Hello everyone. I started a project which is suppose to utilize SOAP API and the URL they provide is a WSDL extension (Here is the URL: http://api.netim.com/2.0/api.wsdl)
Maybe I'm wrong, but WSDL's are suppose to be added as Web References, correct?
I've tried adding as both a service reference and web reference with the URL above and it always returns a 404 error. But, as you can see by visiting the URL above it works fine in a browser. Any idea what I'm doing wrong here? Thanks in advance!
|
|
|
|
|
j'ai une base de donnée Admin et une autre PersonneActive et je souhaiterais recuperer les info dela table Admin pour l'inserer dans la table PersonneActive mais mon soucis c'est qu'il s'enregistre duex et je ne c'est pourquoi.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
If TxtUser.Text = Nothing Or TxtPassWord.Text = Nothing Then
MsgBox("Veuillez renseigner tous les champs", MsgBoxStyle.Exclamation)
Else
If cnx.State = ConnectionState.Closed Then
cnx.Open()
End If
'VERIFER LES ENREGISTREMENTS DANS LA BASE DE DONNEE
'cnx.Open()
Dim searchQuery As String = "select count(*) from Admin WHERE RefAdmin=? and MotDePasse=?"
Dim Command As New OleDbCommand(searchQuery, cnx)
Command.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = TxtUser.Text
Command.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = TxtPassWord.Text
Dim count = Convert.ToInt32(Command.ExecuteScalar())
If (count > 0) Then
MsgBox("Mot de passe validé avec succès'" & TxtUser.Text & "'", MsgBoxStyle.Information)
'DECLARATION DES VARIABLES
Dim motpass, nom, dateactive As String
motpass = TxtPassWord.Text
nom = TxtUser.Text
dateactive = DateAndTime.Now()
Dim insert2 As String = "INSERT INTO [PersonneActive] ([MotPasseActive], [NomActive], [DateActive]) VALUES (@motpass, @nom, @dateactive)"
Using cmd As New OleDbCommand(insert2, cnx)
'cmd.Parameters.AddWithValue("", 1)
cmd.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = TxtPassWord.Text
cmd.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = TxtUser.Text
cmd.Parameters.AddWithValue("@3", OleDbType.VarChar).Value = DateAndTime.Now()
'cmd.Parameters.AddWithValue("@confirmotpass", confirmotpass)
cmd.ExecuteNonQuery()
End Using
TxtUser.Text = ""
TxtPassWord.Text = ""
Me.Hide()
Form1.Show()
Else
MsgBox("Problème avec le mot de passe ou Compte intouvable... Veuillez contacter l'administrateur AGO KOUAME BERTRAND", MsgBoxStyle.Critical)
TxtUser.Text = ""
TxtPassWord.Text = ""
End If
End If
'Dim adapter As New OleDbDataAdapter(Command)
'Dim ds As New DataSet
'Dim table As New DataTable
'adapter.Fill(ds, "CERTIFICAT")
'DataGridView1.DataSource = ds.Tables("CERTIFICAT")
'adapter.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
Finally
cnx.Close()
End Try
End Sub
|
|
|
|