I need some vba help at my Access project.
In our company we got different construction sites.
Each construction site have their own cost centre.
We have to make reports to each construction sites related to the cost centre.
These reports have their own continious number(related to the cost centres)
I got a formular, where I can choose the cost centre and want to be shown the latest report number +1, when making a new input, so no mistakes can happen for users.
Formular looks like this
Nr. is related to the textfield Nummer with the content raumBTBNR for the increasing Report numbers. Kostenstelle got the combo box called KostenstelleAuswahl with the content raeumKostenstelleIDRef for the cost centres.
Code so far: (credits by Gustav)
Private Sub Nummer_GotFocus()
Dim NextNumber As Long
highest Rep.Nr. related table Content of KostenstelleAuswahl
NextNumber = DMax("[raumBTBNR]", "[tbl_Raeumstellen]", "[KostenstellenIDRef] = " & Me!KostenstelleAuswahl.Value & "") + 1
Me!Nummer = NextNumber
End Sub
My solution right now is a query, where i count the number of the reports, related to the cost centres. But it does not fill automatically and is more worse than nice. Which is saved at MaxNr
Example in my form
I would appreciate some help!:)
OK, then this should work - when selecting Kostenstelle:
Private Sub KostenstelleAuswahl_AfterUpdate()
Dim NextNumber As Long
NextNumber = DMax("[raumBTBNR]", "[tbl_Raeumstellen]", "[raeumKostenstelleIDRef] = " & Me!KostenstelleAuswahl.Value & "") + 1
Me!Nr.Value = NextNumber
End Sub
Related
first time asking here because i'm stumped!
I have a MS Access Form with 2 combo boxes:
First combo box (cboPub) selects a Publisher which then filters the second combo box (cboTitle), this works fine however the pulled record is the first record and not the one that meets the criteria.
Code below:
Private Sub cboPub_AfterUpdate()
cboTitle = Null
cboTitle.Requery
End Sub
Sub cboTitle_AfterUpdate()
' Find the record that matches the control.
Me.RecordsetClone.FindFirst "[Supplier] = """ & Me![cboTitle] & """"
Me.Bookmark = Me.RecordsetClone.Bookmark
End Sub
I suspect it is because of the line here:
Me.RecordsetClone.FindFirst "[Supplier] = """ & Me![cboTitle] & """"
but i dont know what to change to have it select the correct record.
A common task is to filter the data in a form's details section from unbound combo boxes in the form header section. (don't set the comboboxs' data sources) I assume that is what you are trying to do because me in the afterupdate event refers to the form. If so set the forms filter rather than messing with the forms record source. Your second problem is cboTitle's afterupdate event doesn't fire when cbotitle's value is changed through vba code. So put all the code in cboPub.
Private Sub cboPub_AfterUpdate()
'cboTitle = dlookup("Title", "SomeTable", "SupplierID = " & cboPub.value) 'how I filtered cboTitle
Me.Filter = "SupplierID = " & cboPub.Value
Me.FilterOn = True
End Sub
Private Sub cboTitle_AfterUpdate()
Debug.Print "check the immediate window for how cboTitle's after update event does not fire when cboTitle is updated with VBA"
End Sub
warning: treat this as pseudo code; the exact filter depends on how Supplier, Publisher, and Title are related in your tables and combo boxes.
Explanation: I think the programmers behind access intentionally forced us to set up our forms entirely at design time. No matter what I have tried Access does not display runtime changes to it's record source in any acceptable fashion. It is the access way or the high way. Linking up your record source and form variables at design time then filtering is the only approach I have found that works. I assume the reasons behind why you cannot trigger an afterupdate event through code are similar.
Suppose you have a microsoft word file (.DOCX), you open it and start to view it, using the mouse scrolling, from page 1 towards the last page.
Then, suppose you see a table, for instance, on page 4.
Now I ask: is it possible for Word-VBA to say to you what is the index or name of that table that is on the screen at the moment, no matter where is cursor located?
I want word-VBA fullfil a table that I am seeing at that moment, no matter where the cursor is located.
I hope I was clear enough...
Word doesn't have a direct way to get what's visible on-screen. It can be calculated, sort of, but not with 100% accuracy.
The following code sample does the trick for me, on my machine. It may need some tweaking to work on a different set up.
The object model does return the co-ordinates of the application window (ActiveWindow, here), the height of that Window and the UsableHeight - the height of the actual document working space. That can be used to get an estimated position.
There's also a Windows API function equivalent - RangeFromPoint - for a Window object that returns the a Range in the document for the given screen co-ordinates.
This code calculates a left and top position for the start of the visible part of the document, as well as for the end of the visible document. (In my test, it was a bit more, but not much). It then checks whether there is one or more tables within that scope. If it is, it takes the first one ( Set tbl = rngTargetStart.Tables(1)) - this returns the object your code needs to work with. As a "bonus", the code prints the index number of the table in the document and the page it's on to the Immediate Window.
Sub CheckForTableOnPage()
Dim WordWindowTop As Long 'in points
Dim WordWindowLeft As Long 'in points
Dim windowUsableHeight As Long 'in points
Dim rngTargetStart As Range
Dim rngTargetEnd As Range
Dim pageNumberTarget As Long
Dim tbl As Table
WordWindowTop = ActiveWindow.height
WordWindowLeft = ActiveWindow.left
windowUsableHeight = ActiveWindow.UsableHeight
RibbonFactor = 200
Set rngTargetStart = ActiveWindow.RangeFromPoint(WordWindowLeft, WordWindowTop - windowUsableHeight)
Set rngTargetEnd = ActiveWindow.RangeFromPoint(WordWindowLeft, WordWindowTop + windowUsableHeight)
rngTargetStart.End = rngTargetEnd.End
If rngTargetStart.Tables.Count >= 1 Then
pageNumberTarget = rngTargetStart.Information(wdActiveEndPageNumber)
Set tbl = rngTargetStart.Tables(1)
rngTargetStart.Start = ActiveDocument.Content.Start
Debug.Print "The table on page " & pageNumberTarget & " is number: " & rngTargetStart.Tables.Count
End If
End Sub
I'm a complete beginner in VB.NET
I have a task to make a form on visual studio and part of it is to make a functional button for report on orders. On navgrid portion of the form I have master object orders list with order id and order date while in datagridview a child object orderslist with details about the order
I need a button that would give me a report on selected order.
I have made a report already and the code for button so far looks like this:
Private Sub BarButtonItem1_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
CR3.OpenReport("arpt_Sales_Orders", "FilterHere")
End Sub
if the FilterHere part is left empty, i get report on ALL the orders with their details.
I have been trying to look for how to filter out what i need but with little success.
I have also tried to look into how to get value from order list (navgrid on the right) but I'm starting to think this is not gonna bring any use. Nevertheless here are my attempts at this so far
Dim vOrderID = atbv_Sales_Orders.CurrentRow(colOrderID)
Dim data As Object = NavGridView.SelectedRow(colOrderID);
Help would be appreciated. I've been on this for hours with little success
EDIT:
To my understanding i need to add to filter section
OrderID = (OrderID value from selected order)
So the question probably stands as, how do i get that value?
Ok, I have finally found the answer myself. Posting it if someone would come across similiar issue.
First row sets event to fire on pressing the button. Next two are what i needed
Private Sub BarButtonItem1_ItemClick_1(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem1.ItemClick
Dim vOrderID As Integer = CInt(cOrderID.Text)
CR3.OpenReport("arpt_Sales_Orders", "OrderID = '" & vOrderID & "'")
End Sub
Works flawlessly for me
I'm making a simple text based adventure in VB.net console that includes combat. In the fight scenes I want the game to generate a random number that determines how much damage you.
This is my current code:
Module Module1
Dim rng As New Random
Dim strmod As Integer = 1
Dim yourhit As Integer
Sub Main()
Console.WriteLine("Welocome to the training grounds!")
yourhit = (rng.Next(1, 4) + strmod)
Console.WriteLine("You hit the dummy and deal {0} damage to it!",
yourhit)
End Sub
End Module
Now i'm wondering if its possible for my randomly generated number to be displayed without it being entered into another variable (in this case the yourhit variable). I've tried simply by having rng instead of yourhit but that a message saying You hit the dummy and deal System.Random damage to it!
I'm new to visual basic so any help would be appreciated!
You should just be able to do:
Console.WriteLine("You hit the dummy and deal {0} damage to it!",
(rng.Next(1, 4) + strmod))
I am completely new to VB.net and have only been learning in for a few weeks
I am doing a project where i need to make an EPOS systems using notepad as a data base. I am able to make the values of the buttons appear in the list box, however I have numerous buttons all with different values but only the first value in the text box is appearing each time a different button is pressed.
E.G
When Heineken button pressed "Heineken €5.00" is displayed
when Guiness button pressed "Heineken €5.00" is displayed
Any help is greatly appreciated!
Imports System.IO
Public Class Form1
Private Sub btnHeineken_Click(sender As Object, e As EventArgs) Handles btnHeineken.Click
Dim sr As IO.StreamReader = IO.File.OpenText("DATABASE.txt")
'File DATABASE.TXT is the the debug folder
Dim name As String
Dim stock, price As Double
name = sr.ReadLine
stock = CDbl(sr.ReadLine)
price = CDbl(sr.ReadLine)
lstBox.Items.Add(name & "" & FormatCurrency(price))
name = sr.ReadLine
End Sub
Private Sub BtnGuiness_Click(sender As Object, e As EventArgs) Handles BtnGuiness.Click
Dim sr As IO.StreamReader = IO.File.OpenText("DATABASE.txt")
'File DATABASE.TXT is the the debug folder
Dim name As String
Dim stock, price As Double
name = sr.ReadLine
stock = CDbl(sr.ReadLine)
price = CDbl(sr.ReadLine)
lstBox.Items.Add(name & "" & FormatCurrency(price))
name = sr.ReadLine
End Sub
DATBASE.txt
Heineken
5.00
20
Guiness
4.50
50
Bulmers
5.00
25
Both your methods have exactly the same code. Thus, they do exactly the same thing: They show the contents of the first entry in your text file.
If you want your methods to do different things, you need to put different code in them.
Unfortunately, putting arbitrary code in your methods won't make them do what you want. It looks like you already discovered that. So the next step is to take a more structured approach:
Decide what your button click should do. It looks like you already did that: You want to display "Guiness €4.50" when the "Guiness" button is clicked.
Next, think about how your program can do that. Apparently, that's where you are stuck. You have a text file with a list of entries, how do you get the one you want?
Translate the result of step 2 (the "algorithm") in code.
You tried to do step 3 before step 2. That won't work, and that's the reason why your code doesn't work.
I suggest that you think really hard about step 2 (How do I find data in a text file? How would I do it if I had the file printed out in front of me and were searching for the data personally?), come up with an algorithm and then return here and ask a new question if you need help translating it to code.