good morning all
I usually find the answer to my queries already on here but this one has me stumped. I have 3 tables,
area containing Area and Region, Phones comtaining phone number and IMSI (SIM number) and a log containing all 4 fields.
I've put in a Dlookup (as found on this site) on the log and although it works the first time the event is triggered it allways comes up with the wrong value. It's probably something stupid and simple, it's years since I used Access and that was probably Access 2007 or something.
Hope you can help. I have created a cut down test version but not sure how to add it to the question.
The VB code is here
Private Sub Area_Change()
Dim FoundRegion As String
FoundRegion = DLookup("[Region]", "[Area]", "'[Area]![Area]=[Area]'")
Me.Region = FoundRegion
End Sub
Private Sub Phone_Number_AfterUpdate()
Dim FoundIMSI As Double
FoundIMSI = DLookup("[IMSI ]", "[phones]", "'[phones]![Phone Number]=[Phone Number]'")
Me.IMSI = FoundIMSI
End Sub
not sure how to add the database
Private Sub Area_Change()
Dim FoundRegion As String
FoundRegion = DLookup("[Region]", "[Area]", "'[Area]![Area]=[Area]'")
Me.Region = FoundRegion
End Sub
Private Sub Phone_Number_AfterUpdate()
Dim FoundIMSI As Double
FoundIMSI = DLookup("[IMSI ]", "[phones]", "'[phones]![Phone Number]=[Phone Number]'")
Me.IMSI = FoundIMSI
End Sub
Related
I have a dataset linked with a MYSQL database via tableadpators and a TableAdpaterManager (all autographed through designer view). The visual studio route was setting up a DataSource and then dragging individual tables onto the form.
Now, I can update, insert and delete records in the database if I edit the datagridviews manually. However, if I remove datarows programatically, the rows are not deleted in the database.
The removal code:
Public Shared Sub RemoveDuplicateRowsViaField(dt As DataTable, RowName As String)
Dim rowList As New List(Of String)
Dim dr As DataRow
For i = dt.Rows.Count - 1 To 0 Step -1
dr = dt(i)
If rowList.Contains(dr.Item(RowName)) Then
dt.Rows.Remove(dr)
Else
rowList.Add(dr.Item(RowName))
End If
Next
dt.DataSet.AcceptChanges()
End Sub
And the 'save' code:
Private Sub SaveOrganisationsBT_Click(sender As Object, e As EventArgs) Handles SaveOrganisationsBT.Click
Me.Validate()
Me.Gi_usersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.dbDS)
End Sub
I've tried many things including looking into the tableAdapters in dataset design view, but am at a loss. Can anyone help?
Nevermind. Solved it. It appears to be around the use of AcceptChanges on the datasource. The code below works. I've read that datarow.remove both deletes the row and accepts changes. I'm guessing if changes are accepted then the tableadapter can't discern it needs deleting from its non 'dirty' state, given changes have been accepted, thus clearing the status of the row? Just supposition. The working code:
Public Shared Sub RemoveDuplicateRowsViaField(dt As DataTable, RowName As String)
Dim rowList As New List(Of String)
Dim dr As DataRow
For i = dt.Rows.Count - 1 To 0 Step -1
dr = dt(i)
If rowList.Contains(dr.Item(RowName)) Then
dr.Delete()
Else
rowList.Add(dr.Item(RowName))
End If
Next
End Sub
I am working on a small project but currently stuck in the process and your help would be much appreciated.
I am trying to display data from one of my SQL tables onto web form (asp), which will effectively be updating as long as data is being entered into the table.
I have managed to get it to work by using the META Tag which refreshes the page every 2 seconds, but I know this is not an ideal way of doing it. It was advised to me to update the web form only from server to client when there is a new inserted value, however I do not know how to approach this.
Please see below to my current code.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Cache Refresh:" & _
Date.Now.ToLongTimeString
SqlDependency.Start(GetConnectionSTring())
Using connection As New SqlConnection(GetConnectionSTring())
Using Command As New SqlCommand(GetSQL(), connection)
Dim Dependency As New SqlCacheDependency(Command)
Dim NumberOfMinutes As Integer = 3
Dim Expires As Date = _
DateTime.Now.AddMinutes(NumberOfMinutes)
Response.Cache.SetExpires(Expires)
Response.Cache.SetCacheability(HttpCacheability.Public)
Response.Cache.SetValidUntilExpires(True)
Response.AddCacheDependency(Dependency)
connection.Open()
GridView1.DataSource = Command.ExecuteReader()
GridView1.DataBind()
End Using
End Using
End Sub
Private Function GetConnectionSTring() As String
Return "Data Source=xxxxxxxxx; Initial Catalog=Test; User ID=xxx; Password= xxx;"
End Function
Private Function GetSQL() As String
Return "SELECT ProductCode, ProductName, Cost FROM dbo.OrderTempTable"
End Function
Thank you for your input.
I think you have to check the DB every certain time.
THIS is for PHP but the idea is the same.
HERE another example.
I'm trying to save myself some time as I update a few similar databases with new data every morning using saved imports. I currently have the following double-click code on a logo which checks to see if I'm the current user of the database (because I'm the only one who will be updating) and if I am the current user, run two saved imports:
Private Sub Image480_DblClick(Cancel As Integer)
Dim sHostName As String
' Get Host Name / Get Computer Name
sHostName = Environ$("computername")
If sHostName = "GS100ZEC" Then
DoCmd.RunSavedImportExport "Import-DocCreator CPD"
DoCmd.RunSavedImportExport "Import-DocCreator CD"
End If
End Sub
The problem seems to be that it is only updating one of these tables with the new data, the second table stays the same. Any tips or something I may have missed would be appreciated!
Update:
Buddy of mine helped me formulate a loop for this function and still same problem, the second import is not running! Code as follows:
Private Sub Image480_DblClick(Cancel As Integer)
Dim sHostName As String
Dim i As Integer
Dim docs(1 To 2) As String
' Get Host Name / Get Computer Name
sHostName = Environ$("USERPROFILE")
docs(1) = "Import-DocCreator CPD"
docs(2) = "Import-DocCreator CD"
If sHostName = "C:\Users\z003nttv" Then
For i = 1 To 2
DoCmd.RunSavedImportExport docs(i)
Next i
End If
End Sub
Also tried to delete the saved import itself and recreating it, still didn't import.
I've got a Windows Form with a button and a datagridview. The project includes a working database connection and LINQ to SQL class. I'm trying to bind the datagridview to the LINQ to SQL.
In a code module I've got this:
Public Function DataGridList() As BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
Return NewBindingSource
End Function
And this button_click code in the form:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
DataGridView1.DataSource = DataGridList()
End Sub
When I click the button I get the length of the school names in the datagridview, with a column header of "length."
If I just run this very similar code in the button_click instead, the school names appear correctly in the immediate window:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim db As New DataClasses1DataContext()
Dim TestQuery =
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
For Each block In TestQuery
Debug.Print(block)
Next
End Sub
Give this a try:
Public Function DataGridList() As BindingSource
Dim NewBindingSource As New BindingSource()
Dim db As New DataClasses1DataContext()
NewBindingSource.DataSource = _
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select New With { Key .Value = Block.gr6_school } Distinct
Return NewBindingSource
End Function
This should give it a property that the DataGridView can pick up on. The New With... creates an anonymous object with a Property named Value. The DataGridView works on this type of object by enumerating the public properties and rendering them as columns. If you had needed more than one value, you could have added additional items inside the curly braces in the same way separated by commas. See Anonymous Types (Visual Basic) for more information.
You might try adding a .ToString to the Select:
From Block In db.BLOCK_ASSIGNMENTs
Where Block.gr912_school = "Franklin"
Select Block.gr6_school.ToString Distinct
I believe that Debug.Print does an implicit conversion to .ToString when it prints to the immediate window. However, a datagrid cell treats everything as an object and displays the default property for that object.
Turns out, of course, this has been addressed often, including on SO, and here. The route I chose was to use an intermediate DataTable:
Public Function DataGridList() As DataTable
Dim NewDataTable As New DataTable
Dim db As New DataClasses1DataContext()
Dim i As Int32
Dim qry =
From Block In db.BLOCK_ASSIGNMENTs.AsEnumerable
Where Block.gr912_school = "Franklin"
Select Block.gr6_school Distinct
NewDataTable.Columns.Add("School")
For i = 0 To qry.Count - 1
NewDataTable.Rows.Add(qry(i))
Next
Return NewDataTable
End Function
This seems pretty slow the first time it runs, so I may try something else in the future, but it allows me to feed the grid via LINQ, which is what I want to work with.
(I would like to use the qry's CopyToDataTable property, but it's only available if the query returns a DataTableRows collection, or some such, and my hacking around didn't reveal how to do that.)
I'm having an issue with report events that I haven't
encountered before in Access prior to Access 2007.
I'm using using Access 2007 for a Front-end to a SQL Back-end.
I have a Class, ReportEvents, that I use for the reports.
In a report's Report_Open event I instantiate and then use this class to
handle events like activate, Close, NoData and I also put common code
such as exporting data to excel instead of a report.
This code was working fine in a previous Access 2003 application (mdb) I was using,
but it isn't working as expected in 2007 (accdb). In my tests the call to a non event public sub, ProvideXLOption works like a charm, but none of the events are being fired
from the ReportEvents class. I didn't change the code I just imported it into the
project. I set up break points but they aren't being hit. I changed all of them to
public events and then called them within the test reports event and they worked fine.
I set up another report in Access 2007 with the same results. I've checked
the startup settings in Access and they are fine. I even removed and re-added
the database location from the trusted locations without any luck.
Has Microsoft modified the Events Code or is this just a simple code error on my part that I'm not seeing? It's gotta be something simple. My brain is just toast (my son decided to stay awake after last night's feeding).
Class ReportEvents Code:
Option Compare Database
Option Explicit
Private WithEvents rpt As Access.Report
Const strEventKey As String = "[Event Procedure]"
Public Property Set Report(Rept As Access.Report)
Set rpt = Rept
With rpt
.OnActivate = strEventKey
.OnClose = strEventKey
If LenB(.OnNoData) = 0 Then
.OnNoData = strEventKey
End If
End With
End Property
Public Sub Terminate()
On Error Resume Next
Set rpt = Nothing
End Sub
Private Sub rpt_Activate()
LoadPrintRibbon
End Sub
Private Sub rpt_Close()
Me.Terminate
End Sub
Private Sub rpt_NoData(Cancel As Integer)
Dim strMsg As String
strMsg = "No Records were found that match your criteria."
MsgBox strMsg, vbInformation, rpt.Name & _
": No Match. Report Cancelled"
Cancel = True
End Sub
Private Sub LoadPrintRibbon()
#If AccessVersion >= 12 Then
If rpt.RibbonName <> "PrintPreview" Then
rpt.RibbonName = "PrintPreview"
End If
#End If
End Sub
';;Provides user with option to send data to Excel instead of a report
Public Sub ProvideXLOption(Cancel As Integer)
'... some XLOption Code
End Sub
In the Test Report Code:
Private Sub Report_Open(Cancel As Integer)
Dim rptEvts As ReportEvents
Set rptEvts = New ReportEvents
Set rptEvts.Report = Me
';;Let User User Choose to Export Data to Excel or Display the report
rptEvts.ProvideXLOption Cancel
End Sub
I figured it out. It was a scope issue The ReportEvents class variable rptEvts, was inside the Report_Open sub. Because of this it wouldn't exist when the other events happened. It should be at the module level and not within the procedure.
Dim rptEvts As ReportEvents
Private Sub Report_Open(Cancel As Integer)
Set rptEvts = New ReportEvents
Set rptEvts.Report = Me
';;Let User User Choose to Export Data to Excel or Display the report
rptEvts.ProvideXLOption Cancel End Sub
End Sub
It's amazing what a little rest will do for you.