How to Update the SetDatasource dynamically on crystal Report - windows

We are using Visual Studio 2015 & SAP crystal reports v13.
Step 1: I’m fetching the Query from the crystal report by using the GetCommandText() function.
Following the code is GetCommandText() function.
Dim boReportDocument As CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim boReportClientDocument As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument = boReportDocument.ReportClientDocument
Dim boReportClientDocument As CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument = boReportDocument.ReportClientDocument
Dim SqlQuery As String = ""
boReportClientDocument.RowsetController.GetSQLStatement(ISCRGroupPath, SqlQuery)
Step 2: I’m trying to concatenate the where condition into the SQL query.
Step 3: Then the Query is Execute and assigned to a dataset.
Step 4: And Set the Dataset value into the CrystalReport.setdatasource.
If I am trying to use the Specific field in the Select query for binding the crystal report, I am getting the attached error.
error image
In the same condition if I am trying to change the Specific filed into Select * From the crystal report is loading correctly. But the “where” condition is not getting considered. The crystal report is loading all the data.
so here is how to update the query into the crystal report. The query is supporting in VB6 & not supporting in vb.net
Following the code is VB.net
Dim ReportDoc As New ReportDocument
Dim ConnectionString = "Data Source=test;Initial Catalog=testDB;User ID=sa;Password=123;"
ReportDoc.Load("~\CrystalReportTesting\CrystalReport1")
Dim con As SqlConnection = New SqlConnection(ConnectionString)
Dim cmd As SqlCommand = New SqlCommand(SqlQuery, con)
Dim adapter As New SqlDataAdapter(cmd)
Dim dtset As New DataSet
adapter.Fill(dtset, "Dataset1")
ReportDoc.SetDataSource(dtset.Tables.Item(0))
CrystalReportViewer1.ReportSource = ReportDoc
CrystalReportViewer1.Refresh()
Following the Code is VB6
fReport.Report.SQLQueryString = fReport.Report.SQLQueryString & _
" where ""Invoice"".""Ref"" in (" & 1,2,3 & ") " & sqlOrderby

Related

Unspecified error while executing sql query vba on Oracle database

I want to import data from Oracle database to Excel using VBA. I've tried several options I found in here or in the official manual but none of them seem to work for me - I always get the same unspecified runtime error on line with
rs.Open strSQL1, con
or
Set rs = con.Execute(strSQL1)
depending on which one of these two methods I use obviously. Here is the whole code:
Sub data_distribution()
'Setting up the database connection
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim strSQL1, strInput, strCon As String
Set con = New ADODB.Connection
Set rs = New ADODB.Recordset
strCon = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST=XXX)(PORT=XXX))" & _
"(CONNECT_DATA=(SERVICE_NAME=XXX))); uid=XXX; pwd=XXX;"
'--- Open the above connection string.
con.ConnectionString = strCon
con.Open
'con.Open strCon
'--- Now connection is open and you can use queries to execute them.
'--- It will be open till you close the connection
'Definition of parameter
strInput = InputBox("Insert car_id")
strSQL1 = "select * from car where car_id = " & strInput & ""
'Executing the query
'rs.activeconnection = con
'rs.Open strSQL1, con
Set rs = con.Execute(strSQL1)
rs.Open strSQL1, con
Worksheets("Data").Range("A2").CopyFromRecordset rs
'Closing connection
rs.Close
con.Close
Set con = Nothing
Set rs = Nothing
I was thinking it might be an issue of connecting to the database but if I import/query data manual, it works just fine. For example using this manual
http://blog.mclaughlinsoftware.com/microsoft-excel/how-to-query-oracle-from-excel-2007/
I had to download and configure ODAC for Windows from Oracle site to make it work. I use tsnames.ora to set the path. Wasn't sure if I configured it right but it works so I guess there isn't a problem with the connection in vba either, or is it? (The variables are, of course, set to real values, not "XXX")
The query itself is correct and returns valid results from the database. Libraries necessary to use ADOBD are linked as well. (Microsoft ActiveX Data Objects 2.0 Library)
There is an existing question with the same error but it's unresolved:
Unspecified run time error while executing vba script
I'm guessing it's station/interface specific (I use Excel 2010, SQL developer).
Even though it's been some time I asked the question, here, it's still unresolved so I will answer myself.
The problem was in the car table where one of the attributes was type CLOB (CHARACTER LARGE OBJECT) with lenght over 2000 characters. Excel was unable to cope with that type and the query caused the unspecified error. If I listed all attributes but this one, it all went well.

VBA Query using ADODB returning different results on different computers

In VBA macro in Excel I have an issue where the exact same query with the exact same database credentials are returning different results depending on what computer the script is being executed. Some pull all of the correct results, others have results missing.
If we execute the query using Toad or any other tool that can execute oracle it has worked correctly on every machine tested.
So far we have found out that the following things are not causing the issue:
Oracle Driver version (instant client vs full both tested no correlation found)
Excel versions (everyone at my company has the same build of excel)
VBA project references are the same.
Confirmed all machines are pointing to the correct database.
ODBC Settings are the same
All machines tested are running Excel 2010 on windows 7, The Database is Oracle 11,
Here is the script
Dim cn
Set cn = CreateObject("ADODB.Connection")
Dim rs
Set rs = CreateObject("ADODB.Recordset")
cn.ConnectionString = "DSN=#####;UID=####;PWD=#####;DBQ=######;DBA=W;APA=T;EXC=F;FEN=T;QTO=T;FRC=10;FDL=10;LOB=T;RST=T;BTD=F;BNF=F;BAM=IfAllSuccessful;NUM=NLS;DPM=F;MTS=T;MDI=F;CSR=F;FWC=F;FBS=64000;TLO=O;MLD=0;ODA=F"`
`cn.Open`
`Set rs = cn.Execute(SQLfinal)`
Dim iCol As Integer
Dim iRow As Integer
fldCount = rs.Fields.Count
For iCol = 1 To fldCount
Sheets("8. Data").Cells(12, iCol).Value = rs.Fields(iCol - 1).Name
Next
Sheets("8. ####").Cells(13, 1).CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Rows("12:12").Select
Selection.AutoFilter
With Selection.Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent1
.TintAndShade = 0.599993896298105
.PatternTintAndShade = 0
End With

VB 2010 retrieve image

Good day sirs. I'm trying to retrieve a photo from my access database then load it in a PictureBox but I have this kind of problem which I can't resolve.
I have seen questions similar to mine but I can't understand the solutions given by others as I'm just a newbie. Will someone please help me correct my codes for retrieving image file from access database. Thanks
I'm using access database and Visual Basic 2010.
Here's the code:
Dim arrImage() As Byte
Dim myMS As New IO.MemoryStream
Dim da As New OleDb.OleDbDataAdapter("SELECT *
FROM tblEmp
WHERE EmployeeID= '"
& Me.txtID.Text
& "'", con)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count > 0 Then
If Not IsDBNull(dt.Rows(0).Item("Picture")) Then
arrImage = dt.Rows(0).Item("Picture")
For Each ar As Byte In arrImage
myMS.WriteByte(ar)
Next
'
inFrm.PictureBox1.Image = Image.FromStream(myMS)
End If
End If
I'm getting a "Parameter is not valid" error from the line
inFrm.PictureBox1.Image = Image.FromStream(myMS)
try replacing the line of code with this
inFrm.PictureBox1.PictureData = myMS.Read

Crystal Reports in VB6 shows up empty for first run

So I'm having a problem with Crystal Reports where the first time I try to run the report, the report shows up empty. The report shows up with the various separators, lines, boxes, etc., but no data to fill in the report. I'm using Visual Basic 6 for the coding. I'm using a lot of inherited code and the code that handles the actual Crystal Reports is a file that is used for other reports and it works fine. So I'm sure the problem is from what I've done where I'm messing something up.
Here is the code I have so far:
Dim rs As ADODB.Recordset
Dim strRptFilePathTemp As String
Dim strRptFileName As String
Dim cSql As String
cSql = "SELECT * FROM TABLE1"
Set rs = DbConn.runStatement(cSql, "rs call", , , , , , , , , , True) 'gets a recordset based on the sql statement above
On Error GoTo ErrHandler
strRptFileName = "ReportName.rpt"
strRptFilePathTemp = App.Path
Screen.MousePointer = vbHourglass
Set frmcrystalreport.ReportRS = rs
DoEvents
frmcrystalreport.reportfile = strRptFilePathTemp & strRptFileName
frmcrystalreport.ReportTitle = _
frmCrystalReportsMainForm.GetRptTitle1("ReportTitle, ") & vbCrLf
gblStrReportFileNameLastRun = frmcrystalreport.reportfile
Screen.MousePointer = vbDefault
DoEvents
frmcrystalreport.Show vbModal
If Not frmcrystalreport.ReportRS Is Nothing Then
frmcrystalreport.ReportRS.Close
Set frmcrystalreport.ReportRS = Nothing
End If
Exit Sub
End If
End Sub
I've tried playing around with the DoEvents function to see if that can help but haven't had much luck with it. Everything works fine after that initial failed attempt at running the report. As long as I don't exit the program, it will print out a report with the valid data once I get passed that blank report. Thanks for any help you guys can give me.
Hmmm, it has been a while since I used VB6 and CR but I sort of remember that your need to discard the saved data before setting the viewers report source
Report.DiscardSavedData
CRViewer1.ReportSource = Report
Just figured out the problem after stumbling upon something in Crystal Reports. I had to turn off the save data with report feature in the report file under the File menu.

Update function on a RecordSet object in VBscript causing DBISAM parse error

I'm having difficulty using the Update function on a RecordSet object while using the DBISAM 4 ODBC driver. Here is what my code looks like.
dtmNewDate = DateSerial(1997, 2, 3)
MsgBox(dtmNewDate)
'Create connection object & connection string
Set AConnection = CreateObject("ADODB.Connection")
strConnection = "Driver={DBISAM 4 ODBC Driver}; CatalogName=S:\RAPID\Z998\2008; ReadOnly=False"
Aconnection.Mode = adModeReadWrite
AConnection.Open strConnection
'create SQL statement to be run in order to populate the recordset
strSQLEmployeeBDate = "SELECT * FROM Z998EMPL WHERE state = 'NY'"
'Create Recordset object
Set rsRecSet = CreateObject("ADODB.Recordset")
rsRecSet.LockType = 2
rsRecSet.Open strSQLEmployeeBDate, AConnection
While Not rsRecSet.EOF
rsRecSet.Fields("BIRTHDATE").value = dtmNewDate
rsRecSet.Update
rsRecSet.MoveNext
Wend
When I try to execute this code I receive the following error:
"DBISAM Engine Error #11949 SQL Parsing error- Expected ( but instead found = in UPDATE SQL statement at line 1, column 336"
I can't figure out what is causing this error. Does anyone have any ideas as to what is causing it?
DBISAM error messages Check the field name.

Resources