Changing connection params when using pass-through query - ms-access-2003

I have an Access 2003 application that I would like to create reports for using stored procedures via pass-through queries. Everything works fine with one exception. When I specify the stored procedure to use for the pass-through query I have to choose a DSN to provide the database connection info. I need to be able to change the connection info for the stored procedure used in the pass-through query via code. This is so I can switch to development, production, testing environments from within the application.
Currently all of my data access (additions, updates, edits) uses ADO and I build the connection strings via VBA code. I am unsure how how to change the connection info of the pass-through queries via code. Any thoughts? Thank you.

Look at the Connect property of your pass-through query. You can change Connect with VBA. This procedure switches between prod and dev database connections.
Public Sub SwitchPassThroughConnection(ByVal pQuery As String, ByVal pTarget As String)
Dim db As DAO.Database
Dim qdef As DAO.QueryDef
Dim strConnect As String
Dim blnError As Boolean
Set db = CurrentDb()
Select Case pTarget
Case "dev"
strConnect = "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=dev;SERVER=cmpq;" & _
"PORT=5432;UID=hans;PWD=changeme;CA=d;A6=;A7=100;A8=4096;" & _
"B0=255;B1=8190;BI=0;C2=dd_;CX=1b502bb;A1=7.4"
Case "prod"
strConnect = "ODBC;DRIVER={PostgreSQL Unicode};DATABASE=prod;SERVER=cmpq;" & _
"PORT=5432;UID=hans;PWD=changeme;CA=d;A6=;A7=100;A8=4096;" & _
"B0=255;B1=8190;BI=0;C2=dd_;CX=1b502bb;A1=7.4"
Case Else
blnError = True
MsgBox "Unrecognized target."
End Select
If Not blnError Then
Set qdef = db.QueryDefs(pQuery)
qdef.Connect = strConnect
qdef.Close
End If
Set qdef = Nothing
Set db = Nothing
End Sub

Related

MS Access VBA DoCmd.OpenForm using where clause not filtering DAO query based recordset

I,m trying to get rid of linked tables and use only VBA code to generate recordset. I found that filtering data using where clause in my DoCmd.OpenForm command doesn't work this way. Is that expected behavior? Or maybe it should work and the problem is located somewhere else... Are OpenArgs the only thing that left me to do this?
To clarify my question:
I have two ms access forms:
One (continuous form) with hyperlink and on click code behind like follows
Private Sub txtPerson_Click()
DoCmd.OpenForm "frmPersonnelDetails", , , "PersonId = " & Me.txtPersonID, acFormReadOnly, acDialog
End Sub
and second one (frmPersonnelDetails), not connected to any recordsource, with recordset created with:
Private Sub Form_Load()
strQuery = "SELECT PersonID, Abbreviation, FirstName, LastName FROM SomeTable"
Set objDaoDb = GetDAODbConn 'function that returns database connection object
Set objDaoRS = objDaoDb.OpenRecordset(strQuery, dbOpenDynaset, dbSeeChanges)
Set Me.Form.Recordset = objDaoRS
End Sub
Now, where clause doesn't work. Second form is opening always on the first record. Is it normal? What is the best way to make it open on specified record?

How to connect to DB2 from Lotus Notes Client via LotusScript?

Just simple using JDBC driver in Java agent works fine. Now I need to connect DB2 from LotusScript. There are many articles like those:
http://www.proudprogrammer.no/web/ppblog.nsf/d6plinks/GANI-9DFMRB
https://openntf.org/XSnippets.nsf/snippet.xsp?id=db2-run-from-lotusscript-into-notes-form
but they use ODBC connection or something else. Anyway I don't see where I can define DB2 host and port in my LotusScript agent. Users won't be able to configure ODBC connection on each workstation. I need some Domino native method to connect to DB2. Or where do I define DB2 host/IP and port in this example:
https://openntf.org/XSnippets.nsf/snippet.xsp?id=db2-run-from-lotusscript-into-notes-form
You could use LSXODBC library but that is deprecated so you probably shouldn't.
The current supported method is to use the LSXLC library but be warned that it provides a very OO-centric approach to sending/consuming data but it is very quick and if you use it as designed, can make moving data from one data provider (say Notes) to another (say DB2) somewhat easy.
If you want to stick with standard SQL strings you can still do that with LSXLC with the "execute" method off of the LSConnection object.
As far as connecting to it goes you just need to make sure the appropriate driver is installed on the machine and then use the appropriate connection parameter in the when creating a new LSConnect object (e.g., ODBC2 for ODBC, DB2 for the CLI DB2 driver, OLEDB for an SQL OLE driver, etc).
If you stick with ODBC or OLEDB you can control the connection string via code. If you use the CLI DB2 driver (which is very, very fast) you need to configure the connection on each machine the driver is installed on.
All this is documented in the Designer help but it is, in my opinion, not organized in the best fashion. But it is all there.
So, some example code that has been largely copied from some code I have sitting around and is not tested is:
Option Declare
UseLSX "*lsxlc"
Sub Initialize
Dim LCSession As LCSession
Dim lcRDBMS As LCConnection
dim lcFieldList as new LCFieldList()
dim lcField_FirstName as LCField
dim lcField_LastName as LCField
dim strFirstName as string
dim strLastName as string
dim strConnectionType as string
' Hard-coding this here just for this example
' I think you will either want an ODBC (odbc2) or a CLI DB2 (db2) connection
strConnectionType = "odbc2"
Set lcRDBMS = New LCConnection (strConnectionType)
' Set some standard properties on the LCConnection object
lcRDBMS.Userid="<userid>"
lcRDBMS.Password="<password>"
lcRDBMS.MapByName=True
' Properties and property values that are different
' depending on the connection type
select case strConnectionType
case "odbc2" :
' Use the DSN name as configured in the ODBC Control Panel (if on Windows)
lcRDMBS.Database = "<SYSTEMDSN>"
case "oledb" :
lcRDBMS.Server = "<myserver.company.net>"
lcRDBMS.Provider = "sqloledb"
lcRDBMS.Database = "<my_database_name>"
' Not sure this actually changes anything or is even setting the correct property
' But the intent is to make sure the connection to the server is encrypted
lcRDBMS.INIT_ProviderString = "ENCRYPT=TRUE"
case "db2" :
' I am afraid I have lost the connection properties we used to use
' to form up a DB2 CLI connection so the following is just a best guess
' But if you are not going to be using the advance features of LSX to
' connect to DB2 you might as well just a standard ODBC driver/connection
lcRDBMS.Database = "<connection_name>"
End Select
Call lcRDBMS.Connect()
' This call returns a status code and populate the lcFieldList object with our results
lngQueryStatus = LcRDBMS.Execute("<Select FirstName, LastName from SCHEMA.Table WHERE blah>", lcFieldList)
If lngQueryStatus <> 0 Then
If lcFieldList_Destination.Recordcount > 0 Then
' Get our fields out of the lcFieldList object before going into the loop.
' Much more performant
Set lcField_FirstName = lcFieldList.Lookup("FirstName")
Set lcField_LastName = lcFieldList.Lookup("LastName")
While (lcConn.Fetch(lcFieldList) > 0 )
strFirstName = lcField_FirstName.Text(0)
strLastName = lcField_LastName.Text(0)
' Do something here with values
Wend
End If
End If
End Sub

VB.NET 2010 RDLC Subreport "Data retrieval failed for the subreport"

Using VB.NET 2010 and SQL Server 2008 R2.
I have inherited an application with a RDLC report. I have not used those before. The report was created and works fine but the originator left out a set of data, a subreport for the "parent" record reported on.
I have tried to add a subreport, point the input parameters of the main to the sub(they use same information). Have the sub use a Dataset that points to a Stored procedure and display the results.
Any attempt to execute (and display) sub report data fails with the message
Data retrieval failed for subreport....located at....Please check the log files for more information.
Off what/where are the log files located?
I can pass in params and as long as I add a dataset w/a stored procedure, I can see the params if I put on report. If I add the stored procedure ...Nothing. I tried a fix found on internet to create a Copy of the XSD's stored procedure call... but that didn't work either.
Main report works fine without Sub.
Thoughts?
What am I missing?
Shouldn't I be able to add a subreport, link up the parameters and have subreport display related information?
Here is my code to call main report:
Dim adapter As New SqlClient.SqlDataAdapter
Dim table As New DataTable
Try
Cursor.Current = Cursors.WaitCursor
ReportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local
ReportViewer1.LocalReport.ReportPath = Application.StartupPath & "\IndividualInterviewerDataReport.rdlc"
Dim ReportParameters(5) As Microsoft.Reporting.WinForms.ReportParameter
ReportParameters(0) = New Microsoft.Reporting.WinForms.ReportParameter("SurveyName", frmMain.SurveyName)
ReportParameters(1) = New Microsoft.Reporting.WinForms.ReportParameter("Interviewer", InterviewerId)
ReportParameters(2) = New Microsoft.Reporting.WinForms.ReportParameter("Panel", PanelMonth)
'
'
ReportParameters(3) = New Microsoft.Reporting.WinForms.ReportParameter("- Version: " + ProductVersion)
ReportParameters(4) = New Microsoft.Reporting.WinForms.ReportParameter("PanelYear", PanelYear)
ReportParameters(5) = New Microsoft.Reporting.WinForms.ReportParameter("SurveyNbr", frmMain.SurveyNum)
ReportViewer1.LocalReport.SetParameters(ReportParameters)
ReportViewer1.ShowPrintButton = True
ReportViewer1.ZoomPercent = 100
Me.spInterviewerDataByPanelTableAdapter.Connection.ConnectionString = My.Settings.DBConnection
Me.spInterviewerDataByPanelTableAdapter.Fill(Me.CodingControlDataSet.spInterviewerDataByPanel, frmMain.SurveyNum, PanelYear, PanelMonth, InterviewerId)
Me.ReportViewer1.RefreshReport()
If I'm remembering correctly, you'll need to handle the SubreportProcessing event of the LocalReport object, and set the subreport data there. You do this by setting a property of the EventArgs parameter for the event handler.
Here's some code (adapted from Microsoft documentation):
'In your report setup code'
AddHandler Me.ReportViewer1.LocalReport.SubreportProcessing, _
AddressOf DemoSubreportProcessingEventHandler
'Event hander
Public Sub DemoSubreportProcessingEventHandler(ByVal sender As Object, _
ByVal e As SubreportProcessingEventArgs)
e.DataSources.Add(New ReportDataSource("DatasetNameInReport", MyDataTable ))
End Sub

Using ASP / VBScript to write to an Excel File

I am trying to take information from a HTML Form and input into an Excel File (xlsx) with ASP / VBScript (not VB.NET). I have some experience in Java and PHP but am new to the VB world.
Sofar I have found ways to get the Data from the GET/POST methods. Now I am trying to create an ADO connection to the excel file.
here is my code so far:
Dim cn as ADODB.Connection
Set cn = New ADODB.Connection
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=EXCEL_FILE.xlsx;" & _"Extended Properties=Excel 12.0 Xml;HDR=YES"
.Open
End With
I got the connection String from here: connectionstrings.com
and tried to stick to this guide: http://support.microsoft.com/kb/257819/en-us
But no luck up until now.
So here are my questions:
1) Is this the right idea in general? So grabbing the Data from POST for example and then opening a connection with ADO to the excel file and adding the info with queries on the connection object?
2) Any obvious flaws in the code ?
3) Would be great if someone could outline a solution, writing data from a HTML Form into an Excel file.
Thanks in advance.
Edit:
Ok Here is the code I try:
Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=EXCEL_FILE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
'From : http://www.connectionstrings.com/excel-2007
.Open
End With
Once I call "Open" on cn it gives me a 500 internal Error. I am not sure if I am making an obvious mistake, but since I don't know where to find error logs I don't have a clue where to start looking.
1 - If you have to use Excel as database, yes it's right. But, if you need a database, should use a database, not an excel sheet.
2 - VBScript doesn't support early binding. You should define variables without data type, should create objects using CreateObject. And you need to some changes in connection string (quotes).
e.g.
Dim cn
Set cn = Server.CreateObject("ADODB.Connection")
With cn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = "Data Source=EXCEL_FILE.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
'From : http://www.connectionstrings.com/excel-2007
.Open
End With
'
' Add new records etc.
'
cn.Close
Set cn = Nothing
3 - An example to add new record (put the instead of above 'Add new records etc.) gets the values from HTML Form (post method).
Dim rs
Set rs = Server.CreateObject("Adodb.Recordset")
With rs
.Open "[Sheet1$]", cn, 1, 3
.AddNew
.Fields(0).Value = Request.Form("Param1") 'Column A1 (or with name rs.Fields("col1").Value = exp )
.Fields(1).Value = Request.Form("Param2") 'Column B1
.Update
.Close
End With
Set rs = Nothing

Enable Save button on different tabs when a form opens

I have a tab control on a form, and a couple different tabs have save buttons on them. Once the user saves data (via SQL statements in VBA), I set the .enabled = false so that they cannot use this button again until moving to a brand new record (which is a button click on the overall form).
so when my form open i was going to reference a sub that enabled all these save buttons because the open event would mean new record. though i get an error that says it either does not exist, or is closed.
any ideas?
thanks
EDIT:
Sub Example()
error handling
Dim db as dao.database
dim rs as dao.recordset
dim sql as string
SQL = "INSERT INTO tblMain (Name, Address, CITY) VALUES ("
if not isnull (me.name) then
sql = sql & """" & me.name & ""","
else
sql = sql & " NULL,"
end if
if not insull(me.adress) then
sql = sql & " """ & me.address & ""","
else
sql = sql & " NULL,"
end if
if not isnull(me.city) then
sql = sql & " """ & me.city & ""","
else
sql = sql & " NULL,"
end if
'debug.print(sql)
set db = currentdb
db.execute (sql)
MsgBox "Changes were successfully saved"
me.MyTabCtl.Pages.Item("SecondPage").setfocus
me.cmdSaveInfo.enabled = false
and then on then the cmdSave needs to get re enabled on a new record (which by the way, this form is unbound), so it all happens when the form is re-opened. I tried this:
Sub Form_Open()
me.cmdSaveInfo.enabled = true
End Sub
and this is where I get the error stated above. So this is also not the tab that has focus when the form opens. Is that why I get this error? I cannot enable or disable a control when the tab is not showing?
You cannot use the form open event to manipulate controls, they have not been initiated at that stage. Use the form load event.
It should never be necessary to set focus to a tab, or even to reference it when working with a control. You will notice that controls must have names unique to the form, and adding a tab does not change this.
I suggest you set some form-level variables: booBtn_1_enabled as Boolean, booBtn_2_enabled as Boolean. Set these to T or F as needed; obviously, all T when the form is opened. Pick a form event (possibly the Current event, but preferably one that is triggered less often) that reviews these variables and sets the controls accordingly:
Me.btnBtn_1.Enabled = booBtn_1_enabled
Me.Repaint
Something like that, but obviously Me.btnBtn_1 may need a more complicated reference.